Hi I Query a Dictionary on C# driver

731 views
Skip to first unread message

Mariano Vicario

unread,
Aug 29, 2011, 11:28:11 AM8/29/11
to mongodb-user
Hi,
I have the following class

public class Group {
[BsonId]
public ObjectID _id {get; set;}
[BsonElement("Me")]
public Members Dictionary<ObjectId, UserGroupProperties> Members
{get; set;}
}

How Do I query on Members property? Where Memebres.ObjectId ==
objectid?

Thanks!
Mariano Vicario

Robert Stam

unread,
Aug 29, 2011, 11:54:45 AM8/29/11
to mongodb-user
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

Mariano Vicario

unread,
Aug 29, 2011, 12:26:14 PM8/29/11
to mongodb-user
Ok....

But it will be in the backlog for version 1.3?

Thanks
Mariano

Robert Stam

unread,
Aug 29, 2011, 12:44:20 PM8/29/11
to mongodb-user
Yes. I've gone ahead and created JIRA ticket for this:

https://jira.mongodb.org/browse/CSHARP-311

Daniel Harman

unread,
Aug 29, 2011, 1:12:54 PM8/29/11
to mongodb-user

Hi Mario,

Robert and I have been discussing this a bit of late and I'm part way
through writing up a 3 part blog on the issues and a custom serializer
to use the alternate scheme Robert mentioned, which I've already
written and tested.

I'll post here when I get the blogs done. Will try today buy may not
have time until later this week.

Dan

Mariano Vicario

unread,
Aug 29, 2011, 1:20:13 PM8/29/11
to mongodb-user
Great Thanks!

I will be waiting

Thanks,
Mariano

Daniel Harman

unread,
Sep 14, 2011, 6:20:41 PM9/14/11
to mongodb-user

Hi Mario,

Apologies for the delay. Part 1 is now up.

http://www.danharman.net/2011/09/14/mongodb-and-c-dictionary-serialisation-part-1-the-problem/

Just need to tidy up the custom serialiser code and will then post it
up for part two soon.

Dan

Mariano Vicario

unread,
Sep 15, 2011, 7:28:35 AM9/15/11
to mongodb-user
Thanks!


On Sep 14, 7:20 pm, Daniel Harman <daniel.a.har...@gmail.com> wrote:
> Hi Mario,
>
> Apologies for the delay. Part 1 is now up.
>
> http://www.danharman.net/2011/09/14/mongodb-and-c-dictionary-serialis...
Reply all
Reply to author
Forward
0 new messages