Getting Started
What is MongoDB?
Install / Config
C.R.U.D. Ops
Data Modeling
Database Mgmt
Create, Read, Update and Delete Operations
Adding Information: the _id field
Should you include or exclude the
_id
field?
.
_id
is like an RDBMS "auto-increment" field
If
_id
is not specified, MongoDB creates this for you
If you specify
_id
it
must
be unique within the collection!
ObjectID is generated by the client
4 byte Timestamp . 3 byte machine ID . 2 byte PID . 3 byte counter
More info: docs.mongodb.org/manual/reference/object-id/
What are the advantages of overwriting the
_id
field?
.
Easier to locate documents
What are the disadvantages of overwriting the
_id
field?
.
Duplicate key error can occur when inserting a document while overwriting the
_id
field
Fatal error: Uncaught exception MongoCursorException with message localhost:27017: E11000 duplicate key error index: mydb.collection.$_id_ dup key: { : ObjectId(528908a9ef5c881f68000000) }
How can you uniquely ID a document without overwriting
_id
?
.
Add an additional field with a your own unique identifying system
+
o
-