Difference between revisions of "MongoDB ObjectIds"
(10 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
== Object IDs == | == Object IDs == | ||
− | + | Object IDs were introduced in section 2 and you may have noticed that the database creates an unique object for each document if you have not created one yourself: | |
db.deptCollection.find().pretty() | db.deptCollection.find().pretty() | ||
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 unique within the collection. Alternatively, you can use the '''ObjectId()''' function to add one, which will be | + | You can assign an objectId yourself to a document, but you do need to ensure it will be unique within the collection. Alternatively, you can use the '''ObjectId()''' function to add one, which again will be unique within the collection. |
The following examples creates a new projectCollection, with two projects: | The following examples creates a new projectCollection, with two projects: | ||
Line 77: | Line 77: | ||
db.projCollection.find({projno: 140}).pretty() | db.projCollection.find({projno: 140}).pretty() | ||
− | Then add a new department using | + | Then add a new department using the object id shown for a project field: |
db.deptCollection.insert( { | db.deptCollection.insert( { | ||
Line 91: | Line 91: | ||
) | ) | ||
− | Where you need to replace ''your_project_id'' with the _id of the project above, for example: 580a8701105d532281cdd3aa" | + | Where you need to replace ''your_project_id'' with the _id of the project above, for example: ''"580a8701105d532281cdd3aa"'' |
+ | |||
+ | |||
+ | === Using Variables === | ||
+ | |||
+ | The above method is not very user-friendly and prone to errors. Alternatively, you can save the object ID into a variable and use that as a value. | ||
+ | |||
+ | First find the object ID: | ||
+ | |||
+ | var oid = db.projCollection.findOne({projno: 140}) | ||
+ | |||
+ | '''findOne()''' must be used instead of '''find()''', since the latter returns a cursor, even if there is only one document that matches the query condition. | ||
+ | |||
+ | Then when adding a new document this variable can be used for the object id: | ||
+ | |||
+ | db.deptCollection.insert( { | ||
+ | deptno: 80, | ||
+ | dname: 'OID Test', | ||
+ | loc: 'WOLVERHAMPTON', | ||
+ | employees: [ { | ||
+ | empno: 9999, | ||
+ | ename: 'Sanjit', | ||
+ | project: oid | ||
+ | } ] | ||
+ | } | ||
+ | ) | ||
== Querying with ObjectIds == | == Querying with ObjectIds == | ||
Line 101: | Line 126: | ||
The ObjectId is a manual [https://docs.mongodb.com/v3.2/reference/database-references/#document-references document] reference. A second query is needed to return the project's results: | The ObjectId is a manual [https://docs.mongodb.com/v3.2/reference/database-references/#document-references document] reference. A second query is needed to return the project's results: | ||
− | db.projCollection.findOne( { _id: ObjectId("your_object_id) }) | + | db.projCollection.findOne( { _id: ObjectId("your_object_id") }) |
+ | |||
== Next step == | == Next step == | ||
Return to the [[MongoDB_Workbook|Workbook]]. | Return to the [[MongoDB_Workbook|Workbook]]. |
Latest revision as of 11:35, 13 November 2017
Main Page >> MongoDB >>MongoDB Workbook >> Object IDs
Object IDs
Object IDs were introduced in section 2 and you may have noticed that the database creates an unique object for each document if you have not created one yourself:
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:
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 unique within the collection. Alternatively, you can use the ObjectId() function to add one, which again will be unique within the collection.
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 } )
If you try and reuse the same objectId:
db.projCollection.insert( { _id: 10, projno: 210, proj_name: 'Other Oracle Project', budget: 10000 } )
You will get a E11000 duplicate key error collection error message.
This is because user generated _ids must be unique within the collection. If you want to reference the _id outside the collection, you need to use a system generated id:
db.projCollection.insert( { projno: 140, proj_name: 'ObjectId Project', budget: 25000 } )
By not defining the _id, the system will generate one automatically.
Check what _id the project has:
db.projCollection.find({projno: 140}).pretty()
Then add a new department using the object id shown for a project field:
db.deptCollection.insert( { deptno: 70, dname: 'OBJECT Test', loc: 'STOCKPORT', employees: [ { empno: 81999, ename: 'Perry', project: ObjectId("your_project_id") } ] } )
Where you need to replace your_project_id with the _id of the project above, for example: "580a8701105d532281cdd3aa"
Using Variables
The above method is not very user-friendly and prone to errors. Alternatively, you can save the object ID into a variable and use that as a value.
First find the object ID:
var oid = db.projCollection.findOne({projno: 140})
findOne() must be used instead of find(), since the latter returns a cursor, even if there is only one document that matches the query condition.
Then when adding a new document this variable can be used for the object id:
db.deptCollection.insert( { deptno: 80, dname: 'OID Test', loc: 'WOLVERHAMPTON', employees: [ { empno: 9999, ename: 'Sanjit', project: oid } ] } )
Querying with ObjectIds
To see our new department:
db.deptCollection.find({deptno: 70}).pretty()
The ObjectId is a manual document reference. A second query is needed to return the project's results:
db.projCollection.findOne( { _id: ObjectId("your_object_id") })
Next step
Return to the Workbook.