Difference between revisions of "MongoDB ObjectIds"

From mi-linux
Jump to navigationJump to search
Line 32: Line 32:
 
== Adding ObjectIds ==
 
== Adding ObjectIds ==
  
You can assign an objectId yourself to a document, but you do need to ensure it will be globally unique, so treating it as a primary key is not a good idea. Instead you can use the '''ObjectId()''' function to add one.
+
You can assign an objectId yourself to a document, but you do need to ensure it will be globally unique, so treating it as a primary key is not a good idea. Alternatively, you can use the '''ObjectId()''' function to add one.
  
Since trying to update individual employee details has caused problems, adding an objectId should help this:
+
The following examples creates a new projectCollection, with two projects:
 
+
  db.projCollection.insert(  
  db.deptCollection.insert(  
+
{
  {
+
  _id: 10,
    deptno: 100,
+
  projno: 110,  
    dname: 'ACCOUNTING',
+
  proj_name: 'Oracle Project',
    loc: 'NEW YORK',
+
  budget: 10000,
    employees: [
+
  }
    {
+
  )
        _id: new ObjectId(),
+
 
        empno: 1111,
+
db.projCollection.insert(
        ename: 'SMYTH',
+
  {_id: 20,
        job: 'MANAGER',
+
  projno: 120,  
        mgr: 7839,
+
  proj_name: 'MongoDB Project',
        hiredate: new Date('2012-08-19'),
+
  budget: 20000,
        sal: 2450
+
   }   
    },
+
)
    {
 
        _id: new ObjectId(),
 
        empno: 2222,
 
        ename: 'WILSON',
 
        job: 'TECHNICAN',
 
        mgr: 7782,
 
        hiredate: new Date(),
 
        sal: 1300
 
      }
 
    ]
 
   }  
 
  )
 
 
 
Check what difference that has made to this new department:
 
 
 
db.deptCollection.find({"deptno":100}).pretty()
 
  
 
== Querying with ObjectIds ==
 
== Querying with ObjectIds ==

Revision as of 20:40, 21 October 2016

Main Page >> MongoDB >>MongoDB Workbook >> Object IDs

Object IDs

You may have noticed that the database creates an unique object for each document:

db.deptCollection.find().pretty()

Examine the output carefully and you will notice for each department in the collection something like:

"_id" : ObjectId("5808e3d2ec0ff55100af2649")

An ObjectId is like a primary key found in relational databases, except in this case it is globally unique across the whole database, not just one table. It is similar to objectIds found in object-oriented programming languages.

ObjectIds created by MongoDB are a 12-byte BSON type, with the following structure:

ObjectID layout
0 1 2 3 4 5 6 7 8 9 10 11
time machine pid inc

Where:

  • time: timestamp - the time in seconds
  • machine: machine identifier
  • pid: process identifier
  • inc: auto-incrementing counter

Adding ObjectIds

You can assign an objectId yourself to a document, but you do need to ensure it will be globally unique, so treating it as a primary key is not a good idea. Alternatively, you can use the ObjectId() function to add one.

The following examples creates a new projectCollection, with two projects:

db.projCollection.insert( 
{
  _id: 10,
  projno: 110, 
  proj_name: 'Oracle Project',
  budget: 10000,
  }
 )
 
db.projCollection.insert(
  {_id: 20,
  projno: 120, 
  proj_name: 'MongoDB Project',
  budget: 20000,
  }  

)

Querying with ObjectIds

db.deptCollection.find({ "employees._id" : ObjectId("580a0e5bf090aeb82af5566c")}).pretty()

Still returns whole array!

Updates with ObjectIds

The objectId can be used to update a record. For example, we want to update employee 2222.