How to do ObjectId serialization in C# Driver 2.3.0-beta1

675 views
Skip to first unread message

Erlend Sand Bærland

unread,
Aug 29, 2016, 8:22:28 AM8/29/16
to mongodb-user
Pre .NET Core in the previous c# driver (2.2.4) I could have a model like this one.

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


Used with a controller it produced the following JSON-schema:

{ "_id": "string"
}

In the latest beta ObjectId is returned like this:

{ "_id": { "timestamp": 0, "machine": 0, "pid": 0, "increment": 0, "creationTime": "2016-08-29T09:32:26.630Z" }
}

How can I have the ObjectId returned as a string?

I'm guessing I'm missing a formatter or an object binder. 

Robert Stam

unread,
Aug 29, 2016, 9:33:50 AM8/29/16
to mongod...@googlegroups.com
I am unable to reproduce what you are describing. I used the following test program:

public class Model
{
    [BsonId]
    public ObjectId _id { getset; }
}
 
public class Program
{
    public static void Main(string[] args)
    {
        var m = new Model { _id = ObjectId.Parse("112233445566778899aabbcc") };
        var json = m.ToJson();
        Console.WriteLine(json);
        Console.ReadLine();
    }
}
And the output was:

{ "_id" : ObjectId("112233445566778899aabbcc") }


If you want the _id to be output as a string you would decorate the _id property with the BsonRepresentation attribute:

public class Model
{
    [BsonId]
    [BsonRepresentation(BsonType.String)]
    public ObjectId _id { getset; }
}
which changes the output to:

{ "_id" : "112233445566778899aabbcc" }

Note: 2.3.0-beta1 does have a  problem with missing dependencies so to get the above to run I had to manually add a dependency on System.Collections.NonGeneric.

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
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+unsubscribe@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/dab91ca2-9d2f-4dce-b716-cdb2277278b8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Erlend Sand Bærland

unread,
Aug 29, 2016, 9:38:00 AM8/29/16
to mongodb-user
I changed my model to: 

public class Model
{
        [BsonId]
        [BsonRepresentation(BsonType.ObjectId)]
        public string _id { get; set; }
}

...which seems to work nicely. 
But, I'm not sure which implications this will have. 

Robert Stam

unread,
Aug 29, 2016, 9:46:08 AM8/29/16
to mongod...@googlegroups.com
The difference between the annotation I tried and the one you tried is just that with my annotation the _id is a an ObjectId in memory but a string in the database, and with your annotation the _id is a string in memory but an ObjectId in the database.

Both are valid depending on your needs.

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
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+unsubscribe@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
Reply all
Reply to author
Forward
0 new messages