0

We have a User doc in Mongo DB and want to use the _id field as the userID field and increment it.
i found a counter("user") JS method in Mongo docs (http://www.mongodb.org/display/DOCS/Object+IDs).

From the Mongo Shell, i can invoke,

db.user.insert({_id:counter("user"), name:"tom"...});

Now, how can i use the same command from Java code? Not able to find a way to specify the Javascript function from Java code.
Also saw the findAndModify Command and usage of $inc, but not able to get it to work. This way we can do without invoking the JS counter func, i presume.

What are the implications of using custom value in the _id field instead of the Mongo's Object Id, any caveats?
~vish

Parvin Gasimzade
  • 25,180
  • 8
  • 56
  • 83
Blue Sky
  • 807
  • 1
  • 15
  • 36

1 Answers1

0

TL;DR: Just use ObjectIds

It makes little sense to generate new ObjectIds like that. You should just create a new ObjectId for each new object. It's easy and offers good global uniqueness guarantees.

If you're doing this to abstract yourself from the database, it won't work. MongoDB is a very specific database. Abstraction is possible if you only use mongo as a K/V store (think memcached, but slower), but there are better products with this functionality.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367