deserialization error related to _id property

89 views
Skip to first unread message

Bob

unread,
Mar 4, 2011, 9:44:59 AM3/4/11
to mongodb-csharp
If I have POCO (plain old c# objects), which do NOT reference MongoDB
libraries, can I use them with the official driver? I can create
objects in mongodb, but fetching them back fails because I dont have
_id property in the object. Can I use some generic type for _id
property, or do I need to use strong ObjectId type? I dont want to
have to reference MongoDB library from those objects if it can be
avoided because they are used in other projects and web services.

To be clear, I get this error:

Unexpected element: _id
at
MongoDB.Bson.DefaultSerializer.BsonClassMapSerializer.Deserialize(BsonReader
bsonReader, Type nominalType, Type actualType,
IBsonSerializationOptions options)

Because, I do NOT have this property on the object:

[BsonId]
public ObjectId _id {get;set;}

So, question is can I avoid above error without implementing such a
property on my objects?

Thanks
Bob

Robert Stam

unread,
Mar 4, 2011, 10:11:42 AM3/4/11
to mongodb-csharp
You can do this, but it takes a few more steps. Basically you:

1. You must have an Id member, but declare it as string instead of
ObjectId
2. Setup the class map so that the Id uses an external representation
of ObjectId

Here's some sample code:

http://www.pastie.org/1632823

If you name your Id field "Id" instead of "MyId" you can omit the call
to SetIdMember.

Also, you must make sure that the string values you assign to MyId are
always valid string representations of an ObjectId. Here's where the
fact that Insert can assign the Id automatically is handy, otherwise
you'd once again have a dependency on ObjectId to create a new
ObjectId.

nicholas brooke-wood

unread,
Mar 5, 2011, 6:19:32 PM3/5/11
to mongodb...@googlegroups.com
we use an Id of type Guid on our pocos, and the deserialiation works fine, we have no reference to the driver

Joseph Good

unread,
Mar 5, 2011, 11:35:38 PM3/5/11
to mongodb...@googlegroups.com
You can also just use one named "Id".  
It does not have to be an ObjectId type.

Robert Stam

unread,
Mar 5, 2011, 11:51:51 PM3/5/11
to mongodb-csharp
True, but if it's not one of these:

1. An ObjectId
2. A Guid
3. A String with an external representation of BsonType.ObjectId

then you have to make sure you assign a value to the Id yourself
before calling Save or Insert.

For these three cases the driver can generate the Id value for you
automatically (which it will only do if you haven't already assigned a
value to the Id).

On Mar 5, 11:35 pm, Joseph Good <joe.d.g...@gmail.com> wrote:
> You can also just use one named "Id".
> It does not have to be an ObjectId type.
>
> On Sat, Mar 5, 2011 at 5:19 PM, nicholas brooke-wood <
>
> nickbrookew...@gmail.com> wrote:
> > we use an Id of type Guid on our pocos, and the deserialiation works fine,
> > we have no reference to the driver
>

Joseph Good

unread,
Mar 6, 2011, 12:08:27 AM3/6/11
to mongodb...@googlegroups.com
It's almost midnight.  Do you sleep?  :-)
Reply all
Reply to author
Forward
0 new messages