Change "_id" to "id"

5,367 views
Skip to first unread message

CJ

unread,
Aug 31, 2012, 9:18:49 PM8/31/12
to mongod...@googlegroups.com
Hi,

I'm wondering if there's a way to configure mongodb to use "id" instead of "_id" on all the entities or bson documents being stored in the dabtabase?

craiggwilson

unread,
Aug 31, 2012, 11:33:51 PM8/31/12
to mongod...@googlegroups.com
No, there isn't.  What is your reasoning for wanting to do this?

Sam Millman

unread,
Sep 1, 2012, 6:33:03 AM9/1/12
to mongod...@googlegroups.com
I agree it is easy to confuse id with _id in your programming especially if you come form SQL (I do it all the time) also id is sometimes cleaner in code however _id is so enbedded now I don't think there is any serious way to change it.

Though I guess it could be a feature to one day be able to define the name of the default primary key in a collection through config.

--
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

CJ

unread,
Sep 1, 2012, 7:52:48 AM9/1/12
to mongod...@googlegroups.com
Sammaye that's a good idea, a configuration file where to define your ways :)

Scott Hernandez

unread,
Sep 1, 2012, 9:24:34 AM9/1/12
to mongod...@googlegroups.com
Generally this is done at the client when you map from bson document
to native object/class.

Russell Bateman

unread,
Sep 1, 2012, 11:21:41 AM9/1/12
to mongod...@googlegroups.com
I use oid, which is merely and painlessly translated to and from _id by my DAO layer (in Java). One need not require the mountain to be moved when there's already a convenient pass right through it.

craiggwilson

unread,
Sep 1, 2012, 1:06:31 PM9/1/12
to mongod...@googlegroups.com
You guys missed the rest of the discussion between CJ and I because he replied via email.  I'll post it here...

So, first off, you can use Id instead of _id.  We (C# driver) will map that by
default.  In addition, you can use [BsonElement("username")] and name
your property with proper C# conventions.  There is also a way to
configure this fluently or with conventions.  I'd suggest you read the
serialization tutorial here:
http://www.mongodb.org/display/DOCS/CSharp+Driver+Serialization+Tutorial

For your JsonWriter question, there is a Strict mode you can use with the JsonWriter in
order to get the MongoDBisms out of the way so things will go straight
into json properly.  However, strict mode will not come back to
mongodb with some tweaking.

On Fri, Aug 31, 2012 at 10:52 PM, CJ <carlo...@gmail.com> wrote:
> Just wanted to have a clean document and was wondering if that was
> possible...
>
> Another issue is i have the following scenario:
>
> public class User{
>     public Guid _id { get; set; }
>     public string username { get; set; }
>     public string password { get; set; }
> public User(){
> _id = Guid.NewGuid();
> }
> }
>
> var user = new User();
>
> // With settings
> JsonWriterSettings settings = new JsonWriterSettings{OutputMode =
> JsonOutputMode.JavaScript };
> user.ToJson(settings);
> /*
> {
>    "_id":{
>       "$binary":"n2FLBkAkhEOCkX42BGXRqg==",
>       "$type":"03"
>    },
>    "username":"",
>    "password":""
> }
> */
> Is there a way that the _id is simply returned
> "_id":"3F2504E0-4F89-11D3-9A0C-0305E82C3301" so that i can use it nicely in
> javascript and the stored document has its _id field in its binary shorter
> counter part ? instead of plain string guid.
Reply all
Reply to author
Forward
0 new messages