Frage

I am having trouble with expressing this query in C# MongoDB, I want it to return all the results of an objectID where it does not equal to "000000000000000000000000" which works in MongoVue; But I can't get it work in my program.

{"ProfilePictureId" : {$ne: new ObjectId ("000000000000000000000000")}}

I am using official C# driver:

var query = new QueryDocument();
foreach (BsonDocument book in col.Find(query))
{
    ...
}
War es hilfreich?

Lösung

You can build your query as follows:

var query = Query.NE("ProfilePictureId", ObjectId.Empty);

ObjectId.Empty returns an ObjectId composed of all zeroes.

Andere Tipps

Assuming that you are querying for documents of a class looking something like:

public class Profile {
        public ObjectId ProfilePictureId { get; set; }
        //... other attributes, construcotrs, methods etc...
}

You can also write your query using expression lambdas like this:

var query = Query<Profile>.NE(s => s.ProfilePictureId, ObjectId.Empty);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top