UberFace
UberFace

Reputation: 485

Mongo C#, query all documents, which array contain a specific element

How do I query in mongo all documents that contain a specific element?

public class House
{
    public Room [] Rooms {get; set;}
}

public class Room 
{
    public string Name {get; set;}
}

I need to build a filter that filter all houses with "bathroom", because I want to set the price of the bathroom to X.

Upvotes: 1

Views: 93

Answers (1)

dnickless
dnickless

Reputation: 10918

The following code should do you what you want:

var collection = database.GetCollection<House>("houses");
collection.Find(new FilterDefinitionBuilder<House>().ElemMatch(house => house.Rooms, room => room.Name == "bathroom"));

Upvotes: 3

Related Questions