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: 
With **mongoengine** create a ObjectId in an embedded document like this:

```
from bson.objectid import ObjectId

class Address(EmbeddedDocument):
    _id = ObjectIdField( required=True, default=ObjectId )
    street = StringField()

```
And here's how to do it in Clojure, assuming the use of congomongo:

```
(import org.bson.types.ObjectId)
(str (ObjectId.)) ; => "12345xxxxx"

```