Rajesh
Rajesh

Reputation: 96

Equivalent of $in in mongo c# driver 2.0

I am using c# driver 2.0 for mongo db. How do I get a list of documents from the database using the $in clause. I couldnt find anything equivalent in the driver documentation.

E.g. To get one album

Album alb =  _collection
            .Find(x => x.ImageId == 1)
            .ToListAsync().GetAwaiter().GetResult();

I would like to get multiple albums in one query. (something like this)

List<Album> albs =  _collection
                .Find(x => x.ImageId "IN (pass in a list of ids)" )
                .ToListAsync().GetAwaiter().GetResult();

Thanks a lot!

Upvotes: 3

Views: 2170

Answers (1)

andrei.ciprian
andrei.ciprian

Reputation: 3025

// IMongoCollection<Album> _collection = ...
var fdb = Builders<Album>.Filter;
var filterIn = fdb.In(x=>x.ImageId, new[] { /*id list*/ });
_collection.Find(filterIn).ToList();

Upvotes: 3

Related Questions