首页 > 解决方案 > 我需要使用过滤的数组项来检索 MongoDB 的对象

问题描述

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

这是我的 2 个文档的示例;

{
        "_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),以检索相同的对象,但在收到的 2 个日期之间过滤了“history_dates”数组。

例如,如果我收到的 2 个日期是:“23/07/2020”和“24/07/2020”,我希望 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 的查询一无所知,我整个星期都在尝试用 Spring Data 来做这个。

更新 1。

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

标签: javaspringmongodb

解决方案


所以基本上你需要做过滤。MongoTemplate为mongodb提供了很多操作,如果某些方法在MongoTemplate中不存在,我们可以使用BsonDocument模式。在这种情况下,试试这篇文章:Trick to covert mongo shell query

实际上,您需要一个 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 文档模式。我还没有在春天测试过这个。


推荐阅读