Aflred
Aflred

Reputation: 4593

Execute Raw Mongodb Query with c# driver v2

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.

Upvotes: 9

Views: 9678

Answers (2)

Prudvi
Prudvi

Reputation: 79

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/

Upvotes: 0

Josh Withee
Josh Withee

Reputation: 11336

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 ] } } } }
                  }"

Upvotes: 6

Related Questions