admin管理员组文章数量:1328029
i expect, that in my view on mongo db, there will be exacly
$and: [
{
create: {
$lte: new Date()
}
}
]
i tried many things with new BsonDocument, literal,BsonJavaScript, BsonString but in the and i had always
$lte: "new Date()"
or
Code("new Date()")
the pipeline script is large, so i dont want to send all as json string
i just want to have new Date() without quotation marks in my view...
part of my code
var pipeline = new List<BsonDocument>
{
new BsonDocument("$match", new BsonDocument("$and", new BsonArray
{
new BasicDBObject("create", new BsonDocument("$lte", new BsonDocument("new Date()"))),
new BasicDBObject("end", new BsonDocument("$gte", new BsonDocument("new Date()")))
}))
...
}
i expect, that in my view on mongo db, there will be exacly
$and: [
{
create: {
$lte: new Date()
}
}
]
i tried many things with new BsonDocument, literal,BsonJavaScript, BsonString but in the and i had always
$lte: "new Date()"
or
Code("new Date()")
the pipeline script is large, so i dont want to send all as json string
i just want to have new Date() without quotation marks in my view...
part of my code
var pipeline = new List<BsonDocument>
{
new BsonDocument("$match", new BsonDocument("$and", new BsonArray
{
new BasicDBObject("create", new BsonDocument("$lte", new BsonDocument("new Date()"))),
new BasicDBObject("end", new BsonDocument("$gte", new BsonDocument("new Date()")))
}))
...
}
Share
Improve this question
edited Feb 13 at 20:22
Dalija Prasnikar♦
28.6k46 gold badges94 silver badges175 bronze badges
asked Dec 5, 2024 at 11:35
IchLiebeKadarkaIchLiebeKadarka
659 bronze badges
3
|
2 Answers
Reset to default 1As mentioned in the comments, you should use a c# DateTime
class. You can see result if you render the pipeline:
var pipeline = new EmptyPipelineDefinition<BsonDocument>()
.Match(new BsonDocument("$and", new BsonArray
{
new BsonDocument("create", DateTime.UtcNow),
new BsonDocument("end", DateTime.UtcNow)
}));
// render for driver version 3.0:
var registry = BsonSerializer.SerializerRegistry;
var rendered = pipeline.Render(new RenderArgs<BsonDocument>
{
SerializerRegistry = registry,
DocumentSerializer = registry.GetSerializer<BsonDocument>()
})
.Documents
.Single()
.ToString();
Output:
{
"$match": {
"$and": [
{
"create": {
"$lte": {
"$date": "2024-12-09T20:19:34.97Z"
}
}
},
{
"end": {
"$gte": {
"$date": "2024-12-09T20:19:34.979Z"
}
}
}
]
}
}
as an option, it's possible to configure date as lazy:
new BsonDocument
{
{ "create", () => DateTime.UtcNow, true }
},
No, i cannot use DateTime.Now, because it will be saved to view as constant date - as you can see in your own output.
i used $$now function - it works as expected, so thanks Joe!
本文标签: mongodbpass mongo function as raw in aggegation query from cStack Overflow
版权声明:本文标题:mongodb - pass mongo function as raw in aggegation query from c# - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742238942a2438556.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
new Date()
? Why not just C#'snew DateTime
? Likenew BsonDocument("$gte", new DateTime())
. Anyway, you can use$$NOW
in aggregations to get the current time. – aneroid Commented Dec 5, 2024 at 12:18