Find where the field is older than 3 days

I have a mongodb database with a data structure like this:

{ 
"_id" : "123456":, 
"Dates" : ["2014-02-05 09:00:15 PM", "2014-02-06 09:00:15 PM", "2014-02-07 09:00:15 PM"],
}

There are other things, but what I would like to do is pull out the data where Dates {$ slice: -1} is older than 3 days. I have such a request, but it does not work:

db.posts.find({}, {RecoveryPoints:{$slice: -1} $lt: ISODate("2014-02-09 00:00:00 AM")})

I'm starting to think that this is impossible, but I decided that I would come here first. I guess I will have to do this logic in my code.

Edit: I would also like to mention that my goal is to return the entire record, not just the date that matches it

+3
source share
1 answer

Your request has all the elements on the projection side, not the request side. Since you do not want to change the appearance of the document, you do not need to design it.

, , "." :

> db.dates.find({ "Dates.0": {$lt: ISODate("2014-02-09 00:00:00 AM") } }).pretty()
{
    "_id" : "123456",
    "Dates" : [
            ISODate("2014-02-05T09:00:15Z"),
            ISODate("2014-02-06T09:00:15Z"),
            ISODate("2014-02-07T09:00:15Z")
    ]
}
> db.dates.find({ "Dates.0": {$lt: ISODate("2014-02-05 00:00:00 AM") } }).pretty()
>

, "." :

> db.dates.find({ "Dates.1": {$lt: ISODate("2014-02-07 00:00:00 AM") } }).pretty()
{
    "_id" : "123456",
    "Dates" : [
            ISODate("2014-02-05T09:00:15Z"),
            ISODate("2014-02-06T09:00:15Z"),
            ISODate("2014-02-07T09:00:15Z")
    ]
}
> db.dates.find({ "Dates.3": {$lt: ISODate("2014-02-07 00:00:00 AM") } }).pretty()
>
> db.dates.find({ "Dates.2": {$lt: ISODate("2014-02-08 00:00:00 AM") } }).pretty()
{
    "_id" : "123456",
    "Dates" : [
            ISODate("2014-02-05T09:00:15Z"),
            ISODate("2014-02-06T09:00:15Z"),
            ISODate("2014-02-07T09:00:15Z")
    ]
}

, , , ( , ) , . , $elemMatch .

, , , - , . , , .

+3

All Articles