简体   繁体   English

使用C#mongo驱动程序执行复杂的mongo JSON查询

[英]Execute complex mongo JSON query using C# mongo driver

I have following mongo shell query: 我有以下mongo shell查询:

{'field':'FieldOne','value':'FieldOne','category':'categoryOne'} , { 'Color' : { '$elemMatch' : { 'Value' : 'Green' } }}

Is there any way to execute same query using C# mongo driver ?. 有没有办法使用C#mongo驱动程序执行相同的查询? I have tried using below C# code, but only the first one is getting executed: 我尝试使用下面的C#代码,但只有第一个执行:

 BsonDocument query = BsonDocument.Parse("{'field':'Overall','value':'Overall','category':'LoggedIncidents'} , { 'Priority' : { '$elemMatch' : { 'Value' : 'P1' } }}");
 QueryDocument queryDoc = new QueryDocument(query);
 var result = collection.Find(queryDoc).ToListAsync().Result;

The first item ( {'field':'FieldOne','value':'FieldOne','category':'categoryOne'} ) is being executed but not the second one ( { 'Color' : { '$elemMatch' : { 'Value' : 'Green' } }} ). 第一项( {'field':'FieldOne','value':'FieldOne','category':'categoryOne'} )正在执行但不是第二项( { 'Color' : { '$elemMatch' : { 'Value' : 'Green' } }} )。

You are using it in find command if i'm not wrong. 如果我没错,你在find命令中使用它。 the first "{}" is what the filter condition for find is. 第一个“{}”是find的过滤条件。 next set of parameters are essentially to show/hide the fields you need. 下一组参数主要是显示/隐藏您需要的字段。 so if you want to filter by value of array "green" you can do this. 因此,如果您想按数组“绿色”的值进行过滤,则可以执行此操作。

 db.objects.find({"color":{$elemMatch:{value:"green"}}})

should give you the results you are looking for. 应该给你你想要的结果。 but if you want to specifically show/hide fields, you can do 但如果你想专门显示/隐藏字段,你可以这样做

 db.objects.find({"color":{$elemMatch:{value:"green"}}},{"field":1})

is this what you want to attain? 这是你想要达到的目标吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM