Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用MongoDB C#驱动程序查询数组_C#_Arrays_Mongodb_Mongodb .net Driver_Mongodb Query - Fatal编程技术网

使用MongoDB C#驱动程序查询数组

使用MongoDB C#驱动程序查询数组,c#,arrays,mongodb,mongodb-.net-driver,mongodb-query,C#,Arrays,Mongodb,Mongodb .net Driver,Mongodb Query,我的文档看起来像: "ID" : "fruit1", "Keys" : [ ["apple", "carrot"] ["banana"] ] 如何使用MongoDB C#驱动程序查询Keys=“carrot” 我可以在shell中完成: db.multiArr.find({'Keys':{$elemMatch:{$elemMatch:{$in:['carrot']}}}}) 我从这里找到的: 但是我没有成功地使用c#driver编

我的文档看起来像:

"ID" : "fruit1",
"Keys" : [
           ["apple", "carrot"]
           ["banana"]
         ]
如何使用MongoDB C#驱动程序查询Keys=“carrot”

我可以在shell中完成:

db.multiArr.find({'Keys':{$elemMatch:{$elemMatch:{$in:['carrot']}}}})
我从这里找到的:


但是我没有成功地使用c#driver编写它。

试试这样的方法

注意:我没有测试这个

MongoClient client = new MongoClient(); // connect to localhost
MongoServer server = client.GetServer();
var db = server.GetDatabase("foo");
var col = db.GetCollection<RawBsonDocument>("multiArr");

// Query = {'Keys':{$elemMatch:{$elemMatch:{$in:['carrot']}}}}
BsonDocument query = new BsonDocument{ 
    "Keys", new BsonDocument {
      "$elemMatch", new BsonDocument {
          "$elemMatch", new BsonDocument {
              "$in", new BsonArray().Add("carrot")
          }
      }
    }
};
col.Find(query);
MongoClient client=new MongoClient();//连接到本地主机
MongoServer server=client.GetServer();
var db=server.GetDatabase(“foo”);
var col=db.GetCollection(“multiArr”);
//查询={'Keys':{$elemMatch:{$elemMatch:{$in:['carrot']}}
BsonDocument query=新的BsonDocument{
“钥匙”,新的B文件{
“$elemMatch”,新的B文档{
“$elemMatch”,新的B文档{
“$in”,新的BsonArray()。添加(“胡萝卜”)
}
}
}
};
col.Find(查询);

更多信息:

我成功地完成了一些不太满的事情:

var q = Query.ElemMatch("Keys", Query.In("$elemMatch", new List<BsonValue> { "carrot" }));
var q=Query.ElemMatch(“key”,Query.In(“$ElemMatch”,新列表{“carrot”}”);

基本上不能以安全的方式复制。如果密钥数组是对象数组而不是字符串数组,如何查询密钥数组?