Inserting New Documents
Every MongoDB document must have unique _id
value. ANd every _id
field in a collection must have a unique value from the rest of the documents in the collection. Likewise, you can have a collection where each document is so distinctly different from the other that they don’t have the same shape or any field names in common.
When we insert a new document, MongoDB populates the _id
field with a value that is of type ObjectId
. The _id
doesn’t have to have the type ObjectId
. It is just what is created by default to ensure unique values for each document. If you already know of unique value that you can use for each document then you can use those values in the _id
field instead.
Inserting One Document At A Time
We can insert a new document using insert
.
For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
db.inspections.insert({
"_id" : ObjectId("56d61033a378eccde8a8354f"),
"id" : "10021-2015-ENFO",
"certificate_number" : 9278806,
"business_name" : "ATLIXCO DELI GROCERY INC.",
"date" : "Feb 20 2015",
"result" : "No Violation Issued",
"sector" : "Cigarette Retail Dealer - 127",
"address" : {
"city" : "RIDGEWOOD",
"zip" : 11385,
"street" : "MENAHAN ST",
"number" : 1712
}
}
)
The above statemtnt will insert a new document in the
inspections
collections if the_id
is a unique value and there are no errors.
Inserting Multiple Documents At A Time
1
2
3
4
db.inspections.insert([
{ "test": 1 },
{ "test": 2 },
{ "test": 3 } ])
Updating Documents
- updateOne(): Used to update only one document at one time. If there are multiple documents that match a given criteria, then only one of them will be updated, whichever one the operation finds first. For example: -
1 2 3 4 5 6 7 8 9 10
db.zips.updateOne( { 'zip': '12534' }, { '$set': { 'pop': 17630 } } )
Here,
$set
is the update operator. In the above query, we are looking to set thepop
field to 17630 in the document where zip = 12534. The$set
update operator sets the field value to a new specified value.$set
syntax also allows us to set multiple fields at the same time by listing the fields and their specified value separated by a comma.
Syntax:
1 2 3 4 5 6 7 {'$set': { 'pop': 17630, '<field2>': <new value>, ... } }
- updateMany(): Used to update multiple documents at one time. It updates all the documents that match the given criteria. For example: -
1 2 3 4 5 6
db.zips.updateMany( {'city': 'HUDSON'}, {'$inc': {'pop': 10} } )
Here,
$inc
is the update operator. In the above query, we are looking to increment thepop
field by 10 in every document which listsHudson
as the city.$inc
syntax also allows us to update multiple fields at the same time by listing the fields and their increment value separated by a comma.
Syntax:
1 2 3 4 5 6 7 {'$inc': { 'pop': 10, '<field2>': <increment value>, ... } }
Just like the $set
and $inc
operator, the $push
is also an Update operator. To add an element to an array field, one of the options is to use the $push
oeprator which has the following syntax:
Syntax:
1 2 3 4 5 6 {'$push': { <field1>: <value1>, .... } }Just like with the set operator, if the field that you specify doesn’t exist in the document then
$push
will add an array field to the document with a specified value.
Example:
1
2
3
4
5
6
7
8
9
10
11
db.grades.updateOne(
{ "student_id": 250,
"class_id": 339 },
{ "$push":
{ "scores":
{ "type": "extra credit",
"score": 100
}
}
}
)
The above query modifies the scores array of the student with
student_id=250
by adding another element to it. In this case, the added element is a document with two field value pairs, type-extra credit and score-100.
Deleting Documents and Collections
- deleteOne(): Deletes one document at a time.
- deleteMany(): Deletes many documents at a time.
To delete a collection use
db.<collection>.drop
. {. prompt-info}