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
  • Your C# API/library is going convert it all to JSON when the pipeline/query is sent to MongoDB anyway. What are you trying to do with new Date()? Why not just C#'s new DateTime? Like new BsonDocument("$gte", new DateTime()). Anyway, you can use $$NOW in aggregations to get the current time. – aneroid Commented Dec 5, 2024 at 12:18
  • so when u use new DateTime() it will be run and compiled, so view will cointain date, when it was run (when migration has been run) – IchLiebeKadarka Commented Dec 5, 2024 at 13:04
  • Have you considered using "$$NOW" or "$currentDate"? – Joe Commented Dec 5, 2024 at 18:11
Add a comment  | 

2 Answers 2

Reset to default 1

As 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