Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have below format mongodb doc

/* 1 */
{
    "_id" : "5f5b1b5d89967c6c48003346",
    "UID" : "5f5b15ca89967c6c4800327d",
    "_createdAt" : "2020-09-11 12:08:21",
},

/* 2 */
{
    "_id" : "5f5b16f789967c6c480032d1",
    "UID" : "5f5b15ca89967c6c4800327d",
    "_createdAt" : "2020-09-11 11:49:35",
},

/* 3 */
{
    "_id" : "5f5b16b389967c6c480032b8",
    "UID" : "5f5b15ca89967c6c4800327d",
    "_createdAt" : "2020-09-11 11:48:27",
},

/* 4 */
{
    "_id" : "5f5b15cd89967c6c48003282",
    "UID" : "5f5b15ca89967c6c4800327d",
    "_createdAt" : "2020-09-11 11:44:37",
}

current time is 2020-09-11 12:10:21

I have to fetch records 10 minutes before _createdAt (current time - 10 minutes).

My expected output

/* 2 */
{
    "_id" : "5f5b16f789967c6c480032d1",
    "UID" : "5f5b15ca89967c6c4800327d",
    "_createdAt" : "2020-09-11 11:49:35",
},

/* 3 */
{
    "_id" : "5f5b16b389967c6c480032b8",
    "UID" : "5f5b15ca89967c6c4800327d",
    "_createdAt" : "2020-09-11 11:48:27",
},

/* 4 */
{
    "_id" : "5f5b15cd89967c6c48003282",
    "UID" : "5f5b15ca89967c6c4800327d",
    "_createdAt" : "2020-09-11 11:44:37",
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
183 views
Welcome To Ask or Share your Answers For Others

1 Answer

Consider your collection name is users and current time is 2020-09-11 12:10:21. So query timestamp would be 2020-09-11 12:00:21.

db.users.find({"_createdAt":{$lt:"2020-09-11 12:00:21"}});

or 

db.getCollection('users').find({"_createdAt":{$lt:"2020-09-11 12:00:21"}});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...