Mongo Id

150 views
Skip to first unread message

FabioEbner

unread,
May 19, 2012, 11:25:19 AM5/19/12
to mongod...@googlegroups.com
Hi, i create one class with morphia.. and put one ObejctID, but i wanna know if i have another ID , something like SGDB id, sequential id, because i need to pass the id to view and than back to front. so i can olny pass the id 


tks

Scott Hernandez

unread,
May 19, 2012, 11:39:22 AM5/19/12
to mongod...@googlegroups.com
You can use any id generation system you want. By default an ObjectID
is suggested since it doesn't require any interaction with the server
to generate and is unique.

You can simply pass the (hex) string version of the ObjectId as the id
to your html/web-app; that works just fine.

If you want to maintain a sequence/counter there are example in the
docs and an example annotation in morphia you can use. It is not
suggested as it requires going to the server before every insert to
get an id.
http://www.mongodb.org/display/DOCS/How+to+Make+an+Auto+Incrementing+Field
http://code.google.com/p/morphia/source/browse/trunk/morphia/src/main/java/com/google/code/morphia/utils/LongIdEntity.java
> --
> 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

Fabio Ebner

unread,
May 19, 2012, 2:27:44 PM5/19/12
to mongod...@googlegroups.com
can you send one example? i use ObjectID to create automatic the id.

2012/5/19 Scott Hernandez <scotthe...@gmail.com>

Scott Hernandez

unread,
May 19, 2012, 4:14:59 PM5/19/12
to mongod...@googlegroups.com
An example of what?

Fabio Ebner

unread,
May 19, 2012, 4:20:51 PM5/19/12
to mongod...@googlegroups.com
how i got from db 

2012/5/19 Scott Hernandez <scotthe...@gmail.com>

Giovanni Fabio

unread,
May 19, 2012, 4:37:21 PM5/19/12
to mongod...@googlegroups.com
SELECT * FROM tablemane WHERE (a=1 OR b=1) AND (a=2 OR b=2)

examples of mongodb query
in mongodb ???

2012/5/19 Scott Hernandez <scotthe...@gmail.com>

Fabio Ebner

unread,
May 19, 2012, 4:41:23 PM5/19/12
to mongod...@googlegroups.com
no..how i use java + morphia to get one data with id.. because i use java + morphi

and my entity i create just like this.

@id
private ObjectId cdProduct;




2012/5/19 Giovanni Fabio <assis...@gmail.com>

Scott Hernandez

unread,
May 19, 2012, 5:34:10 PM5/19/12
to mongod...@googlegroups.com
Datastore ds = ...

instance = ds.get( MyClass.class, id );

http://code.google.com/p/morphia/wiki/Datastore#Get_Methods

Fabio Ebner

unread,
May 20, 2012, 6:18:04 PM5/20/12
to mongod...@googlegroups.com
Yes, but my id is an ObjectID and this ObjectID have a few properties, so in that way i need to pass the complete ObjectID?
i want something i just pass one number, not one object

did you undestand
2012/5/19 Scott Hernandez <scotthe...@gmail.com>

Scott Hernandez

unread,
May 20, 2012, 6:44:05 PM5/20/12
to mongod...@googlegroups.com
If you want to use something other than an ObjectId you can.

See my first response if you want to use morphia with a
auto-incrementing number.

Sam Millman

unread,
Jul 20, 2012, 10:07:45 AM7/20/12
to mongod...@googlegroups.com
"If I use ObjectId as recommended, it creates a complex structure that I do not know how to store in the browser."

Complex structure? It's a string, you make the cookie contents equal the string.

"I used ObjectId.getStringMongod() to convert it to a string value to store in cookie, however now I could not figure out a way to retrieve the object using this string value."

You must recast it as an ObjectId using the string as the constructor param.

On 20 July 2012 11:20, ravi <ravika...@gmail.com> wrote:

I am running into the same problem. Let me try to explain 
I want the unique id to be string, so I can save in browser cookie and retrieve the object when the user comes back.
I would rather not use my own unique Id generation as I am not an expert and can possibly generate duplicates, thus messing up the system.
If I use ObjectId as recommended, it creates a complex structure that I do not know how to store in the browser.
I used ObjectId.getStringMongod() to convert it to a string value to store in cookie, however now I could not figure out a way to retrieve the object using this string value.

Hope that clarifies the issue. 

>> >>> >> > See also the IRC channel -- freenode.net#mongodb
>> >>> >>
>> >>> >> --
>> >>> >> 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
>> >>> >> See also the IRC channel -- freenode.net#mongodb
>> >>> >
>> >>> >
>> >>> > --
>> >>> > 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
>> >>> > See also the IRC channel -- freenode.net#mongodb
>> >>>
>> >>> --
>> >>> 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
>> >>> See also the IRC channel -- freenode.net#mongodb
>> >>
>> >>
>> >> --
>> >> 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
>> >> See also the IRC channel -- freenode.net#mongodb
>> >
>> >
>> > --
>> > 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
>> > See also the IRC channel -- freenode.net#mongodb
>>
>> --
>> 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
>> See also the IRC channel -- freenode.net#mongodb
>
>
> --
> 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
> See also the IRC channel -- freenode.net#mongodb
Reply all
Reply to author
Forward
0 new messages