Reza Akraminejad
Reza Akraminejad

Reputation: 1499

projection wont work in mongodb c# driver

I have class that executes mongo queries its works but when I send projection in query, projection won't work and mongo return hole document

whats the matter?

 query = new QueryDocument( BsonSerializer.Deserialize<BsonDocument>(queryStr));

queryStr="{family:'james'},{}" => its ok

queryStr="{},{family:0}" => not ok. return all columns, but don't want to get family column

Remember: just want this method. not another methods. because want send any query to mongo. I've read mapped mongo objects like ORMs.

just want this method. thanks

Upvotes: 0

Views: 4186

Answers (1)

Nuk Nuk San
Nuk Nuk San

Reputation: 560

Did you look into the new C# 2.0 driver documentation? Looks like projection is a part of the options argument you can give FindAsync

private static void Find(IMongoCollection<Person> mongoCollection)
    {
        var query = Builders<Person>.Filter.Eq(p => p.Name, "bob");

        var options = new FindOptions<Person>()
        {
            Projection = Builders<Person>.Projection
                .Include(p => p.Name)
                .Exclude(p => p.Id)
        };

        var result = await mongoCollection.FindAsync(query, options);

...

The BsonDocument object created from JSON {.A.}{.B.} in the question has 2 braces-pairs, and only the first one will matter (A). This is OK, since projection and query are 2 separate items. Personally

Upvotes: 1

Related Questions