How do I transform this MongoDB Query to a C# Equivalent?
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:
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#. Thanks in advance.
In the C# driver, the field selection is handled by chaining a call to 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?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.