I need to retrieve MongoDB's object just with filtered's array item

大城市里の小女人 提交于 2021-02-15 07:05:44

问题


I'm needing to retrieve just with two dates, all the documents from my MongoDB's collection, with the filtered items from the array.

This is an example of 2 of my documents;

{
        "_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",
}

I'm needing to make a find to my database (Using Spring Data), to retrieve the same objects, but with the "history_dates"'s array filtered between the 2 dates recieved.

For example, if my 2 recieved dates are: "23/07/2020" and "24/07/2020", I want MongoDB to return the next objects;

{
        "_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",
}

I'm really ignorant about MongoDB's queries, and I have been trying to make this with Spring Data all the week.

UPDATE 1.

Thanks varman, and do you know how can i just retrieve the documents with filtered arrays not empty?


回答1:


So basically you need to do filter. MongoTemplate offers a lot of operation for mongodb, if some methods don't exist in MongoTemplate, we can go with Bson Document pattern. In that case, try this article: Trick to covert mongo shell query.

Actually you need a Mongo query something like following. Using $addFields one of the methods shown below. But you can use $project, $set etc. Here $addFields overwrites your history_dates. (It uses to add new fields to document too).

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

Working Mongo playground.

You need to convert this into spring data. So @Autowired the MongoTemplate in you class.

 @Autowired
    MongoTemplate mongoTemplate;

The method is,

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 template doesn't provide add methods for $addFields and $filter. So we just go with bson document pattern. I haven't tested this in Spring.



来源:https://stackoverflow.com/questions/63066314/i-need-to-retrieve-mongodbs-object-just-with-filtereds-array-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!