Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 396
Aflred Avatar asked Jan 15 '18 01:01

Aflred


1 Answers

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 ] } } } }
                  }"
like image 149
Josh Withee Avatar answered Sep 23 '22 12:09

Josh Withee