我需要仅使用过滤的数组项检索MongoDB的对象



我只需要检索两个日期,即MongoDB集合中的所有文档,以及数组中的过滤项。

这是我的两份文件的一个例子;

{
        "_id" : ObjectId("5f18fa823406b7000132d097"),
        "last_date" : "22/07/2020 23:48:32",
        "history_dates" : [
                "22/07/2020 23:48:32",
                "22/07/2020 00:18:53",
                "23/07/2020 00:49:12",
                "23/07/2020 01:19:30"
        ],
        "hostname" : "MyHostname1",
        "ip" : "142.0.111.79",
        "component" : "C:\Windows\System32\es-ES\KernelBase.dll.mui",
        "process" : "LogonUI.exe",
        "date" : "23/07/2020 10:26:04",
}
{
        "_id" : ObjectId("5f18fa823406b7000132d098"),
        "last_date" : "22/07/2020 23:48:33",
        "history_dates" : [
                "22/07/2020 23:48:33",
                "23/07/2020 00:18:53",
        ],
        "hostname" : "MyHostName2",
        "ip" : "142.0.111.54",
        "component" : "C:\Windows\System32\es-ES\KernelBase.dll.mui",
        "process" : "svchost.exe",
        "date" : "23/07/2020 10:26:04",
}

我需要对我的数据库进行查找(使用Spring Data(,以检索相同的对象,但使用";history_dates"'s数组在收到的2个日期之间进行了筛选。

例如,如果我的2个接收日期是:;2020年7月23日";以及";2020年7月24日";,我希望MongoDB返回下一个对象;

{
        "_id" : ObjectId("5f18fa823406b7000132d097"),
        "last_date" : "22/07/2020 23:48:32",
        "history_dates" : [
                "23/07/2020 00:49:12",
                "23/07/2020 01:19:30"
        ],
        "hostname" : "MyHostname1",
        "ip" : "142.0.111.79",
        "component" : "C:\Windows\System32\es-ES\KernelBase.dll.mui",
        "process" : "LogonUI.exe",
        "date" : "23/07/2020 10:26:04",
}
{
        "_id" : ObjectId("5f18fa823406b7000132d098"),
        "last_date" : "22/07/2020 23:48:33",
        "history_dates" : [
                "23/07/2020 00:18:53"
        ],
        "hostname" : "MyHostName2",
        "ip" : "142.0.111.54",
        "component" : "C:\Windows\System32\es-ES\KernelBase.dll.mui",
        "process" : "svchost.exe",
        "date" : "23/07/2020 10:26:04",
}

我真的对MongoDB的查询一无所知,整个星期我都在尝试用SpringData来实现这一点。

更新1。

谢谢varman,你知道我如何才能用不为空的过滤数组检索文档吗?

所以基本上你需要做过滤。MongoTemplate为mongodb提供了大量的操作,如果MongoTemplate中没有一些方法,我们可以使用Bson Document模式。在这种情况下,试试这篇文章:隐藏mongoshell查询的技巧。

实际上,您需要一个Mongo查询,如下所示。使用$addFields的方法之一如下所示。但您可以使用$project$set等。此处$addFields会覆盖您的history_dates。(它还用于向文档添加新字段(。

{
    $addFields: {
        history_dates: {
            $filter: {
                input: "$history_dates",
                cond: {
                    $and: [{
                            $gt: ["$$this", "23/07/2020"]
                        },
                        {
                            $lt: ["$$this", "24/07/2020"]
                        }
                    ]
                }
            }
        }
    }
}

正在Mongo游乐场工作。

您需要将其转换为弹簧数据。所以@Autowired是您类中的MongoTemplate。

 @Autowired
    MongoTemplate mongoTemplate;

方法是,

public List<Object> filterDates(){
    Aggregation aggregation = Aggregation.newAggregation(
        a->new Document("$addFields",
            new Document("history_dates",
                new Document("$filter",
                    new Document("input","$history_dates")
                    .append("cond",
                        new Document("$and",
                            Arrays.asList(
                                new Document("$gt",Arrays.asList("$$this","23/07/2020")),
                                new Document("$lt",Arrays.asList("$$this","24/07/2020"))                            
                            )
                        )
                    )
                )
            )       
        )
    ).withOptions(AggregationOptions.builder().allowDiskUse(Boolean.TRUE).build());
    return mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(YOUR_CLASS.class), Object.class).getMappedResults();
}

Mongo模板没有为$addFields$filter提供添加方法。所以我们只使用bson文档模式。我在春天还没有测试过这个。

最新更新