使用c#驱动程序v2执行Raw Mongodb查询

时间:2018-01-15 01:25:37

标签: c# mongodb mongodb-.net-driver

我有以下代码:

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");

我正在使用标准的mongodb v2驱动程序。

执行查询后,我在

处收到以下错误
db.RunCommand(cmd);

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

我没有丝毫的线索,为什么这不起作用。它在Robo中很有用。

2 个答案:

答案 0 :(得分:2)

您的string cmdDoc格式不正确,无法按照您想要的方式使用它。基于MongoDB docs for the find command,您的字符串应如下所示:

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

或没有用于格式化的所有额外空格:

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

答案 1 :(得分:0)

因为在您的 Mongo Shell 中 db 是指在 mongo Shell 或您正在使用的任何相应工具中默认配置的数据库。

在同一个 shell 或工具中,您甚至可以将 db 更改为 database 或其他内容,并且可以使用 database.collectionName >

如果您将字符串作为输入并指定 db 作为数据库并期待结果。

即使我也尝试过一些更好的结果。

(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/