Professional Documents
Culture Documents
Mongo DB
Mongo DB
• During the insert, mongod will create the _id field and assign it a unique ObjectId
value, as verified by the inserted document:
• The ObjectId values are specific to the machine and time when the operation is
run.
• As such, your values may differ from those in the example.
Insert a Document Specifying an _id
Field
• In the following example, the document passed to the
insert() method includes the _id field.
• The value of _id must be unique within the collection to
avoid duplicate key error.
• db.products.insert( { _id: 10, item: "box", qty: 20 } )
• The operation inserts the following document in the
products collection:
• { "_id" : 10, "item" : "box", "qty" : 20 }
Insert Multiple Documents
• The following example performs a bulk insert of three
documents by passing an array of documents to the insert()
method.
• By default, MongoDB performs an ordered insert.
• With ordered inserts,
• if an error occurs during an insert of one of the documents,
• MongoDB returns on error without processing
The documents the do
in the array remaining
not need to have the
documents in the array.
db.products.insert(
same fields.
[
For instance, the first document in the array has
{ _id: 11, item: "pencil", qty: 50, type: "no.2" },
an _id field and a type field.
{ item: "pen", qty: 20 },
Because the second and third documents do not
{ item: "eraser", qty: 25 }
contain an _id field,
]
mongod will create the _id field for the second and
)
third documents during the insert
Output
• The operation inserted the following three
documents:
{ "_id" : 11, "item" : "pencil", "qty" : 50, "type" : "no.2" }
{ "_id" : ObjectId("51e0373c6f35bd826f47e9a0"), "item" : "pen", "qty" : 20 }
{ "_id" : ObjectId("51e0373c6f35bd826f47e9a1"), "item" : "eraser", "qty" : 25 }
Perform an Unordered Insert
•update()
•updateOne()
•updateMany()
•replaceOne()
Update Operators
•$set •$currentDate
•$unset •$pop
•$rename •$addToSet
•$inc
•$mul
Update method syntax
update() updateOne() updateMany()
db.<collection>.method(<query>, <update>, <options>)
$set
• Update the GPA of sID 123 to 4.2
• db.student.update(
{"sID":123}, //Query
{$set:{ //update
"GPA":4.2
}
},
{} //update options (Optional)
)
$set
• $set could also be used to add new fields and well as update
multiple fileds
• db.student.update(
{"sID":123},
{$set:{
"sizeHS":1001, "GPA":4.0, "placed":"yes"
}
}
)
$set
• $set use to update a document
• Update the grade od sID 123 in Big Data to D+
• db.student.update(
{sID:123, "marks.sub":"Big Data"},
{$set:
{"marks.$.grade":"D+“
}
}
)
$addToSet
• Add an email id amy123@gla.ac.in of sID 123
• db.student.update(
{sID:123},
{$addToSet:
{"email":"amy123@gla.ac.in"}
}
)
$addToSet
• Add an email id amy123@yahoo.com and amy.cse@fb.com of
sID 123
• db.student.update(
{sID:123},
{$addToSet:
{"email":"amy123@gla.ac.in"}
}
)
$unset
• $unset use to delete a particular field
• db.student.update(
{"sID":123},
{$unset:{
"placed":""
}
}
)
Query Documents
• Select All Documents in a Collection
• To select all documents in the collection, pass an empty document
as the query filter parameter to the find method. The query filter
parameter determines the select criteria:
• db.inventory.find( {} )
db.inventory.insertMany( [
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);
Match an Embedded/Nested Document
• the following query selects all documents where the field
size equals the document { h: 14, w: 21, uom: "cm" }
• db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )
• Equality matches on the whole embedded document require
an exact match of the specified <value> document, including
the field order.
• For example, the following query does not match any
documents in the inventory collection:
• db.inventory.find( { size: { w: 21, h: 14, uom: "cm" } } )
Specify Equality Match on a Nested
Field
• The following example selects all documents where the field
uom nested in the size field equals "in":
• db.inventory.find( { "size.uom": "in" } )
• To specify a query condition on fields in an embedded /
nested document,
• use dot notation ("field.nestedField").
Specify Match using Query Operator
• A query filter document can use the query operators to
specify conditions in the following form:
• { <field1>: { <operator1>: <value1> }, ... }
• The following query uses the less than operator ($lt) on the
field h embedded in the size field:
• db.inventory.find( { "size.h": { $lt: 15 } } )
Specify AND Condition
• The following query selects all documents where the nested
field h is less than 15, the nested field uom equals "in", and
the status field equals "D":
• db.inventory.find( {
• "size.h": { $lt: 15 },
• "size.uom": "in",
• status: "D"
• })