查询 MongoDb 以获取小于 NOW 的日期时间值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29791171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:24:01  来源:igfitidea点击:

Query MongoDb for a datetime value less than NOW

mongodbdatetime

提问by Matt Evans

Thisquestion addressess generating a query from C#, but how does one generate a comparision value equivalent to GetDate() (SQL Server) in MongoDB

这个问题解决了从 C# 生成查询的问题,但是如何在 MongoDB 中生成与 GetDate() (SQL Server) 等效的比较值

i.e. I would like to express a JSON query like this:

即我想表达一个这样的 JSON 查询:

{
Expiration: {$lte:  Now())},
}

回答by Pascal Bugnion

Use the Javascript function new Date():

使用 Javascript 函数new Date()

db.collection.find({ Expiration: { $lte: new Date() } })

The Mongo shell is just a Javascript shell, so you can, in principle, use any Javascript method.

Mongo shell 只是一个 Javascript shell,因此原则上您可以使用任何 Javascript 方法。

回答by Vishwas

You can use any javascript function in query of mongo.

您可以在 mongo 查询中使用任何 javascript 函数。

Try this :

尝试这个 :

db.collection.find({"Expiration":{"$lte": new Date()}})