Mongo Document Id

12 views
Skip to first unread message

ajinkya prabhune

unread,
Oct 14, 2011, 8:55:30 AM10/14/11
to mongod...@googlegroups.com
Hello

I have been working with mongo for a long time now.
But I was curious to know that whether it is possible to have the auto generated doc _id 
before a document is inserted in the mongodb ?

I am using Mongo Java Library.



--
Thank you and Regards
Ajinkya Prabhune

Karl Seguin

unread,
Oct 14, 2011, 9:02:36 AM10/14/11
to mongod...@googlegroups.com
org.bson.types.ObjectId has a static "get" method. You can use that to get the id and assign it to your objects explicitly. The driver will know that this is a new id and do an insert rather than an update.

Karl

Stuart Johnson

unread,
Oct 14, 2011, 9:08:05 AM10/14/11
to mongod...@googlegroups.com
You can create your own _id if you want. Insertion just generates one
for you if it's not included in your document. _id usually has the
inserton time embedded into it, but I sometimes use it as an alternative
to a separate indexed Date/Time field, and set it accordingly.

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

Sam Millman

unread,
Oct 14, 2011, 9:27:32 AM10/14/11
to mongod...@googlegroups.com
There are ways in many languages to know this however I will talk about PHP cos tbh that's the only one I know when it comes to Mongo (haven't used Mongo with other languages). I am sure there are similar bindings in all languages.

This will allow you to have an _id prior to insertion and use that _id to force the _id of that document to that value:

$id = new MongoId(); // Lets make a new _id
$db->insert(array('_id' => $id, 'name' => 'sam')) // Lets save a document with this id

Then you can use the $id to know the id before insert.

After insert doc will have _id field too.

On 14 October 2011 14:08, Stuart Johnson <stu...@stu.org.uk> wrote:
You can create your own _id if you want.  Insertion just generates one for you if it's not included in your document.  _id usually has the inserton time embedded into it, but I sometimes use it as an alternative to a separate indexed Date/Time field, and set it accordingly.




On 14/10/11 13:55, ajinkya prabhune wrote:
Hello

I have been working with mongo for a long time now.
But I was curious to know that whether it is possible to have the auto generated doc _id
before a document is inserted in the mongodb ?

I am using Mongo Java Library.



--
Thank you and Regards
Ajinkya Prabhune

--
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+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
--
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+unsubscribe@googlegroups.com.

ajax

unread,
Oct 17, 2011, 9:32:15 AM10/17/11
to mongodb-user
Hello Guys,

Sorry for the late reply....
I am looking into ur suggestions.
and thanks for the help it has given me a guideline to do further
work.

thanks
Reply all
Reply to author
Forward
0 new messages