MongoDB C#查询小于当前时间的时间戳 [英] MongoDB C# query for timestamp less than the current time
本文介绍了MongoDB C#查询小于当前时间的时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从id = 1且创建日期小于当前时间的集合中返回文档.
I'm trying to return documents from the collection where id = 1 and date created is less than the current time.
我尝试了这个,但是没有用:
I tried this, but it isn't working:
var collection = database.GetCollection("test");
var time = DateTime.Now;
var query2 = new QueryDocument
{
{ "id", 1},
{{"created_on", {"$lt",time}}
};
此查询出了什么问题?
推荐答案
您需要$lt
子对象的嵌入式文档,但是却忘记了创建它:
You need an embedded document for the $lt
sub-object, but you forgot to create it:
var query = new QueryDocument {
{ "id", 1 },
{ "created_on", new BsonDocument { { "$lt", time } } }
}
还可以考虑使用Query
构建器,这可能会使事情变得更简单:
Also consider using the Query
builder, which might make things simpler:
var query = Query.And(
Query.EQ("id", 1),
Query.LT("created_on", time)
);
这篇关于MongoDB C#查询小于当前时间的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文