KDecker
KDecker

Reputation: 7148

Finding BsonDocuments where a specific fields value is within a List<ValueType>?

I have two collections in my MongoDB database. Collection1 has a field of type Guid (well string really) named Col2DocRef which references the _id of documents in Collection2.

I currently have a List<Guid> representing _ids of documents in Collection2. I would like to find all documents in Collection1 where the field Col2DocRef is equal to any value in the List<Guid>.

Is this the proper way to do this by using AnyIn?

List<Guid> guids = ... // Creating by searching in Collection2 and deserializing
Builders<BsonDocument>.Filter.AnyIn("Col2DocRef", guids);

Upvotes: 0

Views: 338

Answers (1)

Rahul
Rahul

Reputation: 77896

You can directly use FindAsync() passing the filter like below assuming that those collection have corresponding strongly typed POCO in your application interface.

List<Guid> guids = ... // Creating by searching in Collection2 and deserializing
var collection = db.GetCollection<Collection1>("Collection1");
var result = collection.FindAsync(col => guids.Contains(col.Id)).ToListAsync();

Upvotes: 1

Related Questions