$not operator in query

26 views
Skip to first unread message

Grégoire Seux

unread,
Jul 7, 2011, 6:13:06 AM7/7/11
to mongodb...@googlegroups.com
Hello,

even if I am quite used now to formulating query with the C# driver, I am still fighting with expressing this one : 
{fileGUID : {$not :{$elemMatch : {counter : {$ne : 0}}}}}

Basically, it means that I am looking for entries whose subdocuments in fileGUID  have all their counter field to zero.

I tried to use the QueryNot class but does not understand how to use it :-(

Any pointer ?

Thanks by advance !

Grégoire

Robert Stam

unread,
Jul 7, 2011, 9:20:19 AM7/7/11
to mongodb-csharp
Your query can be written in C# as:

var query = Query.Not("fileGUID").ElemMatch(Query.NE("counter",
0));

A good way to check whether that produces the right query or not is:

var json = query.ToJson();
Console.WriteLine(json);

which outputs:

{ "fileGUID" : { "$not" : { "$elemMatch" : { "counter" : { "$ne" :
0 } } } } }

Grégoire Seux

unread,
Jul 7, 2011, 9:25:22 AM7/7/11
to mongodb...@googlegroups.com
Thank you very much !


I did temporarly with
string queryString = "{fileGUID : {$not :{$elemMatch : {counter : {$ne : 0}}}}, state : \"importing\"}";
            var queryDoc = BsonSerializer.Deserialize<BsonDocument>(queryString);
            QueryComplete finishedImportingQuery = new QueryComplete(queryDoc);

but your way is far better !

Gregoire
Reply all
Reply to author
Forward
0 new messages