2. CRUD and Mongo Shell
CRUD which stands for an operation of creating, read, update, and delete documents.
In Mongo DB, we called these operations in different names:
Create – Insert
Read – Find
Update – Update
Delete – Remove
All of these operations are function-based methods, which means that Mongo DB’s CRUD operations exist as methods/functions in programming language APIs, not as a separate language like T-SQL.
2.1 Create Operations
Create or insert operations add new documents to a collection. The MongoDB provides the following methods:
- db.collection.insert()
- db.collection.insertOne() new in version 3.2
- db.collection.insertMany() new in version 3.2
In MongoDB, insert operations target a single collection. All write operations in MongoDB are atomic on the level of a single document
For more information on atomicity, transaction and concurrency control will be discussed in later chapters.

In the new version 3.2, there are two functions are added – insertOne() and insertMany(). What’s the differences between three of these functions?
Behavior
If the collection does not exist, insert operations will create the collection.
Returns
Insert(): returns a WriteResult object, like WriteResult({“nInserted”: 1 }). The nInserted field specifies the number of documents added. If the operation encounters an error, the WriteResult object will contain the error information.
InsertOne(): returns a document with the status of the operation:
{
"acknowledged": true,
"insertedId": ObjectId("5742045ecacf0ba0c3fa82b0")
}
InsertMany(): returns a document with the status of the operation:
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("57420d48cacf0ba0c3fa82b1"),
ObjectId("57420d48cacf0ba0c3fa82b2"),
ObjectId("57420d48cacf0ba0c3fa82b3")
]
}
2.2 Query Documents
Query Method
- db.collection.findOne()
- db.collection.find() this method returns a cursor to the matching document.
db.collection.find(<query filter>, <projection>)
Return only one document
If we just want to get one document from your collection. db.collection.findOne() is the best choice.

Select All Document in a Collection
db.collection.find({}) or db.collection.find()
Specify Query Filter Conditions
Specify Equality Condition
<field> : <value> expressions are used to select all documents that contain the <field> with the specified <value>:
{<field1> : <value1>, <field2> : <value2>, …}
Specify Conditions Using Query Operators
A query filter document can use the query operators to specify conditions in the following form:
{ <field1>: { <operator1>: <value1> }, … }
$in : Either … Or … operator (comparison operators)

OR Conditions
$or : a logical OR conjunction so that the query selects the documents in the collection that match at least one condition.

AND Conditions
Implicitly, a logical AND combination connects the clauses of a compound query so that the query selects the documents in the collection that match all the conditions.

Specify AND as well as OR Conditions

Using regex and $exists
$exists

$regex

Query on Embedded Documents
When the field holds an embedded document, a query can either specify an exact match on the embedded document or specify a match by individual fields in the embedded document using the dot notation.
db.users.find( { favorites: { artist: &quot;Picasso&quot;, food: &quot;pizza&quot; } } )
db.users.find( { &quot;favorites.artist&quot;: &quot;Picasso&quot; } )
Query on Arrays
When the field holds an array, you can query for an exact array match or for specific values in the array. If the array holds embedded documents, you can query for specific fields in the embedded documents using dot notation.
If you specify multiple conditions using the $elemMatch operator, the array must contain at least one element that satisfies all the conditions.
