Question: So I need to reference particular subdocuments uniquely from items in my collection. For instance:

```
User = {
    'name': 'jim',
    'documents: [
        {'id': 0001, 'title': "My document"},
        {'id': 0002, 'title': "My second document!"},
    ]
}

```

So I need to be able to auto-create IDs for new documents, preferably not at the application level (since there will be race conditions in the actual development scenario).

Is there a way to use mongo's autogenerated ObjectId (used in the \_id field at the collection level), or something similar?

Answer: 
All drivers have functionality for generating ObjectIds.

In the shell you just do `new ObjectId()`:

```
> db.test.insert({x:new ObjectId()});
> db.test.find();
{ "_id" : ObjectId("4f88592a06c05e4de90d0bc1"), "x" : ObjectId("4f88592a06c05e4de90d0bc0") }

```

In Java it's `new ObjectId()` as well. See the API docs for your driver to see the specific syntax.
And this is how you can do it in python (pymongo):

```
from pymongo import MongoClient
import bson

client = MongoClient('mongodb://localhost:27017/')
db = client.test_db

result=db.users.insert_one({'name': 'jim',
    'documents': [
        {'_id': bson.objectid.ObjectId(), 'title': "My document"},
        {'_id': bson.objectid.ObjectId(), 'title': "My second document!"},
    ]})

print list(db.users.find({}))

```