There is no way to do a contains with the Mongo LINQ driver
Hi Vijay,
The problem here is the way you express Not Contains(). For example, given the following documents in a collection:
{"_id": "a", "Reference": "foo"},
|
{"_id": "b", "Reference": "bar"},
|
{"_id": "c", "Reference": "baz"},
|
{"_id": "d", "Reference": "qux"}
|
Based on your example, let's declare an example class Foo as below:
public class Foo
|
{
|
public string Id {get;set;}
|
public string Reference {get;set;}
|
}
|
Utilise the following snippet LINQ expression to achieve the same way:
string[] ignoredIds = new string[1] {"b"};
|
var documents = (from p in collection.AsQueryable<Foo>() where
|
p.Id !=null &&
|
!ignoredIds.Contains(p.Id) &&
|
p.Reference != "baz"
|
select p).ToList();
|
Which returns the following documents (where Id is not null and Id is not in ignoredIds and Reference is not baz)
{ "_id" : "a", "Reference" : "foo" }
|
{ "_id" : "d", "Reference" : "qux" }
|
The above snippet should work for both MongoDB .NET/C# driver v2.5 and v2.7.
Please note that the CSHARP project is for reporting bugs or feature suggestions for the MongoDB .NET/C# driver. If you have any follow-up questions on the use of the driver, please post a question on mongodb-user group with relevant the information.
Regards,
Wan.
|