Insert a new document using the Atlas User Interface.
_id
value. This value must be unique for each document<aside> 💡 You can take the M320 Data Modelling course for MongoDB.
</aside>
Insert a document using the mongo shell
use <database name>
db.inspections.findOne()
command in the shell which will return a random document from that collectionE11000
code if the document to be inserted does not have a unique _id
_id
field, then MongoDB automatically generates one for you.<aside> 💡 MongoDB also has a Schema Validation functionality that allows you to enforce document structure
</aside>
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
}
})
Multiple Documents can also be inserted at a time, We can do this by passing a list of documents to the insert command
db.inspections.insert([
{"name" : "value"},
{"name" : "value"},
{"name" : "value"},
])
{"ordered": false}
in the insert method.db.inspections.insert([
{"_id" : "value"},
{"_id" : "value"},
{"_id" : "value2"},
], {"ordered": false})
Documents can be easily updated from MongoDB Atlas easily using the user interface
Updating documents using the mongo shell
updateOne()
and updateMany()
pop
) to every city of the New York State.db.zips.updateMany(
{"state": "NY"},
{"
$inc": {
"pop": 10
}
}
)
$inc
operator, there are many others out of which some are stated below:
$set
- Sets the specified value$push
- Adds an element to an array field