Reputation: 31
I am new to MongoDb so I just played around with it.
Right now I have created a MongoDbCollection<MyDocument>
which stores a lot of MyDocuments
. MyDocument
holds many different parameters like ID
, Name
, Birthdate
, xData
, ...
class MyDocument
{
public MyDocument() { }
public ObjectID Id { get; set; }
public string Name { get; set; }
public string Birthdate { get; set; }
public BsonDocument[] xData { get; set; } // just additional data
}
If I query the MongoDbCollection
for documents...
MongoCursor elements = collection.Find(Query.And(MyQueryInput));
for example: a query which is looking for name "Peter". I'll get a list with all elements where 'name' equals "peter". It also returns the 'name' field from the "xData" BsonDocument
. So far so good everything works as wanted.
Now I want to create a MongoDbCollection<List<MyDocument>>
, so its a collection of lists which hold MyDocuments
. Thats why I defined a new class which holds the MongoID
and a List
:
class MyMongoList
{
public MyMongoList() { }
public ObjectID Id() { get; set; }
public List<MyDocument> list { get; set; }
}
I can add MyDocuments
to the list and lists to the collection, but if I loop through nothing happens.
MongoCursor elements = collection.Find(Query.And(MyQueryInput));
foreach(MyMongoList<MyDocument> list in elements)
{
Console.WriteLine("something"); // nothing happends here
}
I am wondering why "elements" is empty.
[Question] Is it possible to query all lists like above?
[Question] Do I have to add something to the MyMongoList class that MongoDb can search in c#Lists?
At the moment, I do not know what's wrong. I have the feeling that it has to do with MongoDb Serialization but until now I did not find a way to implement it.
Upvotes: 0
Views: 98
Reputation: 31
I've found my mistakes. I just have to use the Query.ElemMatch() method. An example was explained here.
to query a list I used the following code:
MongoCursor elements = collection.Find(Query.ElemMatch("list",MyQueryInput));
Now MongoDB puts all lists from the collection, which possess at least 1 MyDokument matching the criterion, into elements.
Upvotes: 1