How to do $in query in c# using LINQ

2,815 views
Skip to first unread message

Bob

unread,
Mar 3, 2011, 10:17:00 AM3/3/11
to mongodb-csharp
I want to fetch all records from a collection in one request, given a
list of keys (like a list of document IDs). So I want to use C#
driver to do the following type of query (probably using some form of
LINQ query):

db.Documents.find({DocumentId:{$in: ["a","b","c"]}});

I'm not sure what type of Linq queries are supported with the
driver.

Thanks.
Bob

Robert Stam

unread,
Mar 3, 2011, 10:37:16 AM3/3/11
to mongodb-csharp
With the official C# driver you would write the query this way:

var documentIds = new BsonValue[] { "a", "b", "c" };
var query = Query.In("DocumentId", documentIds);
foreach (var document in collection.Find(query)) {
// process document
}

Support for querying using LINQ will be added post v1.0.

craiggwilson

unread,
Mar 6, 2011, 11:27:15 AM3/6/11
to mongodb-csharp
Which driver are you using Bob?
Reply all
Reply to author
Forward
0 new messages