9

I have the following code :

MongoClient m = new MongoClient();
        var db = m.GetDatabase("PopupRentals");



        string cmdDoc = (@"
                            db.Rentals.find({polygons:
                                             {$geoIntersects:
                                                 {$geometry:{ 'type' : 'Point',
                                                      'coordinates' : [ 17.3734, 78.4738 ]
                                }
                            }
                                              }
                                         });");

        var cmd = new JsonCommand<BsonDocument>(cmdDoc);

        var res = db.RunCommand(cmd);

        var result = db.GetCollection<RentalProperty>("Rentals");

I am using the standard mongodb v2 driver.

Once i execute the query i get the following error at about the

db.RunCommand(cmd);

System.FormatException: 'JSON reader was expecting a value but found 'db'.'

I have not the slightest clue why this is not working. It works great in Robo.

2 Answers 2

6

Your string cmdDoc is not in proper format for using it the way you want to use it. Based on the MongoDB docs for the find command, your string should look like this:

string cmdDoc = @"
                    {
                      find: "Rentals",
                      filter: {
                                polygons: {
                                            $geoIntersects: {
                                                              $geometry: { 
                                                                           'type' : 'Point',
                                                                           'coordinates' : [ 17.3734, 78.4738 ]
                                                                         }
                                                            }
                                          }
                              }
                    }"

or without all the extra whitespace for formatting:

string cmdDoc = @"{
                    find: "Rentals",
                    filter: { polygons: { $geoIntersects: { $geometry: { 'type' : 'Point', 'coordinates' : [ 17.3734, 78.4738 ] } } } }
                  }"
Sign up to request clarification or add additional context in comments.

Comments

0

Since in your Mongo Shell db is referring to the Database configured by default in mongo Shell or any corresponding tool which you are using.

In the same shell or tool you can even change that db to database or something else and can use like database.collectionName

where in case while you are passing string as input and specifying db as database and expecting results.

Even I aslo tried for some better results.

(i) With the mongoDB C# driver, how do I issue a runCommand?

(ii)

 var _server = new MongoClient(new MongoUrl("mongodb://localhost:27017"));
        var db = _server.GetDatabase("DBName");
        var cmdRes =
        @"{
                'aggregate': 'collectionName',
                'allowDiskUse': true,
                'pipeline':[
                    {
                        '$match':{
                            '_id':'5e2b3328d53dc61f2cc6f54c'
                        }
                    }
                ],
                'cursor': { 'batchSize': 25 }
        }";
        var v = db.RunCommand<object>(cmdRes);
        Console.WriteLine(JsonConvert.SerializeObject(v));

https://docs.mongodb.com/realm/sdk/dotnet/examples/mongodb-remote-access/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.