c# serialization, how to deserialize document to registered map class

2,145 views
Skip to first unread message

mamu

unread,
Jan 13, 2011, 11:39:17 AM1/13/11
to mongodb-user
I am trying to implement read/write using new mapclass functionality.
I can't figure out exactly how it should be used, document covers
almost everything but looks like skipped very basic step.

Below is what i think it should be used and what i don't know.

How to serialize? using .ToBsonDocument() will document using
registered class map

How to deserialize? I can't figure out how to deserialize document.

Can someone please explain how it should be serialized and
deserialized using classmap?

Robert Stam

unread,
Jan 13, 2011, 12:15:27 PM1/13/11
to mongodb-user
Unless you are trying to do something out of the ordinary you can let
your classes be automapped and not worry about creating the class maps
yourself.

Here is some sample code from an earlier similar question:

http://www.pastie.org/1456904

It shows example of using Insert, FindOne, Save and Update with a non-
trivial C# class. Serialization and deserialization happens
automatically as part of the Insert, Save, Update and FindOne
commands.

mamu

unread,
Jan 13, 2011, 12:25:47 PM1/13/11
to mongodb-user
That works when you have one to one mapping with class for a document,
In our case document is quite large and object i am trying to get is
only subset of document. So i am running query returning selected
fields. Also as document changes i am trying to handle null and do not
exists cases which can be handled using class map.

Above is one case where i need to deserialize document using class
map. Another one is just for bson serialization purpose not involving
mongo where i just have bson document i need to convert to a class
object.

Robert Stam

unread,
Jan 13, 2011, 1:44:41 PM1/13/11
to mongodb-user
You can get just part of a result document as a BsonDocument by using
the SetFields method of the cursor. Here's an example to get just the
HomeAddress of a Student:

var query = Query.EQ("_id", student.Id);
var fields = Fields.Include("HomeAddress");
var document =
collection.FindAs<BsonDocument>(query).SetFields(fields).SetLimit(1).FirstOrDefault();

You can further deserialize that result into a C# object of type
Address (using the classes in the sample code):

var address =
BsonSerializer.Deserialize<Address>(document["HomeAddress"].AsBsonDocument);

Note that you have to pass the value of "HomeAddress" to Deserialize,
not the entire document.

You can serialize an object to a BSON byte stream and back using:

byte[] bson = student.ToBson();
student = BsonSerializer.Deserialize<Student>(bson);

Manish Patel

unread,
Jan 14, 2011, 2:21:47 PM1/14/11
to mongod...@googlegroups.com
Deserialization part does not work, there is no method takes BsonDocument and deserialize it. I am using latest mongodb driver.


           var address =
BsonSerializer.Deserialize<
Address>(document["HomeAddress"].AsBsonDocument);


--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.




--

Thanks,
Manish Patel

Robert Stam

unread,
Jan 16, 2011, 6:05:22 PM1/16/11
to mongodb-user
There is such a method. It is declared as:

public static T Deserialize<T>(
BsonDocument document
)

in BsonSerializer.

However, this method was introduced since v0.9 was released, so you
either would have to use the latest code from master or wait for v0.11
to come out soon.
> > mongodb-user...@googlegroups.com<mongodb-user%2Bunsu...@googlegroups.com>
> > .

Robert Stam

unread,
Feb 14, 2013, 10:24:04 AM2/14/13
to mongod...@googlegroups.com
Can you be a bit more specific? Can you show the full class definition and a sample JSON document showing what you want the document to look like in the database?

It sort of sounds like you want to map two fields in your POCO to a single combined string in the JSON document, but I'm not sure.

On Thu, Feb 14, 2013 at 8:56 AM, Siva <siva...@gmail.com> wrote:
Hi Robert,

   I have  written a class which has the fields name,Reg_No,etc.

        public int Reg_No { get; set; }
        public string Name { get; set; }
my case reg no will be like " 1, be01 " in mongo db the field type will be int32 for value 1 and string for be01

in my class i have tried both string and int datatype, I'am facing the following error.
An error occurred while deserializing the Reg_No property of class Mongo_Studentattendance: Input string was not in a correct format.

Please help me to solve this and guide me to proceed further.

thanks in advance.

--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Siva

unread,
Feb 15, 2013, 1:19:13 AM2/15/13
to mongod...@googlegroups.com
Hi Robert Stam,

     I have imported data from sql via csv to mongodb, in that Reg_No has values like numeric and string, when I try to map with a class with type string for the field Reg_No, getting error input string was not in a correct format.
{
   Reg_No : 1
}
{
  Reg_No : "s1"
}

can you please tell me how to make all values in field "Reg_No" type as string for value  Reg_No: 1 also.

Siva

unread,
Feb 15, 2013, 9:09:10 AM2/15/13
to mongod...@googlegroups.com
I tried this  [link](http://stackoverflow.com/questions/4973095/mongodb-how-to-change-the-type-of-a-field) answer given by Simone it worked. thanks @Robert Stam .
Reply all
Reply to author
Forward
0 new messages