1

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))
{
    ...
}

2 Answers 2

8

You can build your query as follows:

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

ObjectId.Empty returns an ObjectId composed of all zeroes.

Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.