Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

how to fetch 10 minutes before created records in mongodb

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

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

1 Reply

0 votes
by (71.8m points)

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"}});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...