Auto increment or Object id?

1,124 views
Skip to first unread message

Maddy

unread,
Feb 4, 2011, 4:30:46 AM2/4/11
to mongodb-user
Hi all,

i have a embedded document (Nested collection) for each nested
document i need to store an auto increment or object id generated by
Mongo is that any way i can do this. i have a class structure like
this

Public Class Comment_1
Private _id As MongoDB.Bson.ObjectId
Public Property Id() As MongoDB.Bson.ObjectId
Get
Return _id
End Get
Set(ByVal value As MongoDB.Bson.ObjectId)
_id = value
End Set
End Property

Private _Message As String
Public Property Message() As String
Get
Return _Message
End Get
Set(ByVal value As String)
_Message = value
End Set
End Property

Private _Reply As List(Of Comment_1)
Public Property Reply() As List(Of Comment_1)
Get
Return _Reply
End Get
Set(ByVal value As List(Of Comment_1))
_Reply = value
End Set
End Property
End Class


while i am inserting the collection i am doing as such

Dim objComment1 As New Comment_1
objComment1.Id = New MongoDB.Bson.ObjectId() // creating
object id
objComment1.Message = "Hurrya"

the problem i am facing is i am able to insert the objectid for the
main document but in the nested document the objectid is Storing like
"_id": "000000000000000000000000", how to store the object id in
nested document also. ? or is it possible for me to store the auto
increment number generated by DB itself. i heard its wise to use the
Object id instead of auto increment while sharding? Any solution for
my problem.

Thanks & Regards

Maddy

unread,
Feb 4, 2011, 4:47:27 AM2/4/11
to mongodb-user
How to Create New object id from application level. i Mean passing
Time stamp, Machine Id, etc... is there any syntax available for C#
or .Net

Nat

unread,
Feb 4, 2011, 5:00:04 AM2/4/11
to mongodb-user
you should use GenerateNewId() method to create new ObjectId instead
of New ObjectId to generate a unique id.

Maddy

unread,
Feb 4, 2011, 5:24:17 AM2/4/11
to mongodb-user
Thanks a lot. its working fine. as i expected.

Thanks a lot for all the your efforts for answering my doubts (even
silly doubt sometimes)
Reply all
Reply to author
Forward
0 new messages