This type of query is probably not possible at the moment. To
understand why, let's look at the resulting document:
> db.test.findOne()
{
"_id" : ObjectId("4e5bb404e447ad2c3450d895"),
"Me" : [
[
ObjectId("4e5bb404e447ad2c3450d893"),
{
"X" : 1,
"Y" : 2
}
],
[
ObjectId("4e5bb404e447ad2c3450d894"),
{
"X" : 3,
"Y" : 4
}
]
]
}
>
Note: I've had to create a fake UserGroupProperties class that has two
properties: "X" and "Y".
The key issue here is that the Dictionary<ObjectId,
UserGroupProperties> property has been represented as an array of
nested two element arrays. The nested two element arrays contain the
key and the value of the dictionary items.
While this representation is perfectly acceptable for serializing and
deserializing, it is difficult to query against because MongoDB does
not really have any way to query against nested array elements.
This has come up before and a really good suggestion has been to
change how Dictionary is serialized. Suppose that instead it was
serialized like this:
> db.test.find().skip(1)
{ "_id" : ObjectId("4e5bb404e447ad2c3450d896"), "Me" : [
{
"k" : ObjectId("4e5bb404e447ad2c3450d897"),
"v" : {
"X" : 1,
"Y" : 2
}
},
{
"k" : ObjectId("4e5bb404e447ad2c3450d898"),
"v" : {
"X" : 3,
"Y" : 4
}
}
] }
>
The difference being that the array of dictionary items is now an
array of nested documents (instead of nested two element arrays) where
the elements are named "k" and "v" (short for "key" and "value").
With this new representation your query is easy:
> db.test.find({"Me.k" : ObjectId("4e5bb404e447ad2c3450d898")})
{ "_id" : ObjectId("4e5bb404e447ad2c3450d896"), "Me" : [
{
"k" : ObjectId("4e5bb404e447ad2c3450d897"),
"v" : {
"X" : 1,
"Y" : 2
}
},
{
"k" : ObjectId("4e5bb404e447ad2c3450d898"),
"v" : {
"X" : 3,
"Y" : 4
}
}
] }
>
However, this new representation won't be in the v1.2 driver that will
be released soon, so you'd have to wait for the v1.3 release.
Let me know if you have any comments regarding the proposed changed to
how a Dictionary is serialized.
Thanks,
Robert