Search code examples
c#mongodb

pass mongo function as raw in aggegation query from c#


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()")))
     }))
...
}

Solution

  • 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!