it (iterate) to iterate through the cursor results in mongo shell
// Connect to the Atlas database
mongo "mongodb+srv://<username>:<password>@<cluster>.mongodb.net/admin"
// Show all databases
show dbs
// Select database
use sample_training
// Show all collections
show collections
// db points to the current database
db.zips.find({"state": "NY"})
// return the total number of the documents
db.zips.find({"state": "NY"}).count()
db.zips.find({"state": "NY", "city": "ALBANY"})
// Easier to read
db.zips.find({"state": "NY", "city": "ALBANY"}).pretty()
Inserting New Document – ObjectId
Identical document can exist in the same collection as long as their _id values are different
MongoDB has schema validation functionality allows you to enforce document structure
Data Explorer
Every document must have a unique _id value
ObjectId() – default value for the _id field otherwise specified
Mongo Shell
// Get random document from the collection (to copy the data structure)
db.inspections.findOne();
// Insert
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
}
})
db.inspections.find({"id" : "10021-2015-ENFO", "certificate_number" : 9278806}).pretty()
Inserting New Documents (multiple documents)
Pass an array to the .insert() function
By default, the documents are added according to the order in the given array. When the duplicated _id detected, the insertion operation quits. Add {“ordered”: false} to prevent it.
// increment field value by a specified amount
{ "$inc": { "pop": 10 } }
// sets field value to a new value
{ "$set": { "pop": 17630 } }
// adds an element to an array field
{"$push": { "scores": { "type": "extra credit","score": 100 }}}
Delete Documents & Collections
Data Explorer
Mongo Shell
Collection
db.<collection>.drop()
Documents that match a given query
deleteOne(“_id”: 11)
deleteMany()
MQL operators
Update Operators
Modify data in the database
Example: $inc, $set, $unset
Query Operators
Provide additional ways to locate data within the database