简体   繁体   English

使用C#的MongoDB查询字段选择

[英]MongoDB query field selection using C#

How do I transform this MongoDB Query to a C# Equivalent? 如何将此MongoDB查询转换为C#等效项?

db.lists.find({_id: 10}, {planet_sizes: {$elemMatch: {id: 1}}})

I've tried the following without success, which means that it doesn't return the same results as what I get in the shell: 我尝试以下操作均未成功,这意味着它不会返回与在shell中得到的结果相同的结果:

  IMongoQuery searchQuery = Query.And(
             Query.EQ("_id", 10),
             Query.ElemMatch("planet_sizes",
             Query.EQ("id", 1)));

I want query the main list of document and extract the document with _id 10, and from its array, extract the array item with id equals to 1. The MongoDB string query that I provided above works in shell, but I don't know how to write an equivalent one in C#. 我想查询文档的主列表,并使用_id 10提取文档,然后从其数组中提取ID等于1的数组项。我在上面提供的MongoDB字符串查询在shell中有效,但是我不知道如何用C#编写等效的代码。 Thanks in advance. 提前致谢。

In the C# driver, the field selection is handled by chaining a call to SetFields : 在C#驱动程序中,通过SetFields的调用链接SetFields来处理字段选择:

var docs = db.GetCollection("list")
    .Find(Query.EQ("_id", 10))
    .SetFields(Fields.ElemMatch("planet_sizes", Query.EQ("id", 1)))
    .ToList();

您可以始终使用linq http://docs.mongodb.org/ecosystem/tutorial/use-linq-queries-with-csharp-driver/或只想使用MongoQuery?

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

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