1

I'm using MongoDB Atlas and creating search pipeline using C# dictionary object then serializing it to json and then parsing the json string to BsonDocument.

In $search stage I'm using near object in compound should with origin date for sorting.

As you can see I'm creating an object for near in which my origin is DateTime.UtcNow

Here is my function which converts Dictionary objects to BsonDocument

This is my BsonDocument after converting Dictionary objects to Json and then converting to Bson

Here I'm using it in agreegation pipeline

The main problem is mongodb is not accepting C# DateTime as a date in origin.

Please guide me how can i give proper MongoDb date from my C# code.

1 Answers1

0

welcome to Stack Overflow. Maybe you need to convert your near origin parameter specifically with BsonDateTime prior to conversion of the entire object, like so:

new BsonDateTime(origin)

In the future it is easier for us to assist if you paste the code rather than the picture whenever possible.

Nice-Guy
  • 1,457
  • 11
  • 20
  • I’ve tried what you’ve told me but still it is throwing same error. You can see my code var nearCreateDateObject = new NearClauseSearchModel { path = “CreatedDate”, origin = new BsonDateTime(DateTime.Today.Date), pivot = Convert.ToUInt64(“7776000000”), score = GenericObject(“boost”, GenericObject(“value”, createDateScore)) }; and this is the final bson. “near”:{ “path”:“CreatedDate”, “origin”:“2021-04-17T19:00:00Z”, “pivot”:“NumberLong(”“7776000000"”)", “score”:{ “boost”:{ “value”:1 } } } – Waleed Nasir Apr 18 '21 at 12:40
  • can you format with "`"? That makes it easier to read. Can you edit your original post with the code? It will make it easier to answer the question. – Nice-Guy Apr 19 '21 at 05:38
  • I've solved this error the way you told me & it works. Thank you :) – Waleed Nasir Nov 24 '21 at 20:39
  • can you upvote and accept the answer please? :) – Nice-Guy Nov 26 '21 at 23:11
  • Accepted and upvoted. :) – Waleed Nasir Nov 29 '21 at 11:15