© Subhashini Chellappan and Dharanitharan Ganesan 2020
S. Chellappan, D. GanesanMongoDB Recipeshttps://doi.org/10.1007/978-1-4842-4891-1_2

2. MongoDB CRUD Operations

Subhashini Chellappan1  and Dharanitharan Ganesan2
(1)
Bangalore, India
(2)
Krishnagiri, Tamil Nadu, India
 
In Chapter 1, we discussed MongoDB features and how to install MongoDB on Windows and Linux. We also discussed terms used in MongoDB and how to create a database. In this chapter, we are going to discuss how to perform create, read, update, and delete (CRUD) operations in MongoDB. This chapter also describes how to work with embedded document and arrays. We are going to discuss the following topics:
  • Collections.

  • MongoDB CRUD operations.

  • Bulk write operations.

  • MongoDB import and export.

  • Embedded documents.

  • Working with arrays.

  • Array of embedded documents.

  • Projection.

  • Dealing with null and missing values.

  • Working with limit() and skip().

  • Working with Node.js and MongoDB.

Collections

MongoDB collections are similar to tables in RDBMS. In MongoDB, collections are created automatically when we refer to them in a command. For an example, consider the following command.
db.person.insert({_id:100001,name:"Taanushree A S",age:10})

This command creates a collection named person if it is not present. If it is present, it simply inserts a document into the person collection.

Recipe 2-1. Create a Collection

In this recipe, we are going to discuss how to create a collection.

Problem

You want to create a collection named person using the db.createCollection() command.

Solution

Use the following syntax to create a collection.
db.createCollection (<name>)

How It Works

Let’s follow the steps in this section to create a collection named person.

Step 1: Create a Collection

To create a collection by the name person use the following command.
db.createCollection("person")
Here is the output,
> db.createCollection("person")
{ "ok" : 1 }
>
To confirm the existence of the collection, type the following command in the mongo shell.
> show collections
person
>

We can now see the collection named person.

Recipe 2-2. Create Capped Collections

In this recipe, we are going to discuss what a capped collection is and how to create one. Capped collections are fixed-size collection for which we can specify the maximum size of the collection in bytes and the number of documents allowed. Capped collections work similar to a circular buffer: Once it fills its allocated space, it makes room for new documents by overwriting the oldest documents in the collection.

There are some limitations of capped collections:
  • You can’t shard capped collections.

  • You can’t use the aggregation pipeline operator $out to write results to a capped collection.

  • You can’t delete documents from a capped collection.

  • Creating an index allows you to perform efficient update operations.

Problem

You want to create a capped collection named student using the db.createCollection() method .

Solution

Use the following syntax to create a collection.
db.createCollection (<name>,{capped:<boolean>,size:<number>,max:<number>})

How It Works

Let’s follow the steps in this section to create a capped collection named student.

Step 1: Create a Capped Collection

To create a capped collection named student , use the following command.
db.createCollection("student",{capped: true,size:1000,max:2})

Here, size denotes the maximum size of the collection in bytes and max denotes the maximum number of documents allowed.

Here is the output,
> db.createCollection("student",{capped: true,size:1000,max:2})
{ "ok" : 1 }
>
To check if a collection is capped, type the following command in the mongo shell.
> db.student.isCapped()
true
>
We can see that the student collection is capped. Now, we will insert few documents into the student collection, as shown next.
db.student.insert([{ "_id" : 10001, "name" : "Taanushree A S", "age" : 10 },{ "_id" : 10002, "name" : "Aruna M S", "age" : 14 }])
Here is the output,
> db.student.insert([{ "_id" : 10001, "name" : "Taanushree A S", "age" : 10 },{ "_id" : 10002, "name" : "Aruna M S", "age" : 14 }])
BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 2,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})
>

Step 2: Query a Capped Collection

To query a capped collection, use the following command.
db.student.find()
Here is the output,
> db.student.find()
{ "_id" : 10001, "name" : "Taanushree A S", "age" : 10 }
{ "_id" : 10002, "name" : "Aruna M S", "age" : 14 }
>
Here, MongoDB retrieves the results in the order of insertion. To retrieve documents in the reverse order, use sort() and set the $natural parameter to -1 as shown here.
> db.student.find().sort({$natural:-1})
{ "_id" : 10002, "name" : "Aruna M S", "age" : 14 }
{ "_id" : 10001, "name" : "Taanushree A S", "age" : 10 }
>

We can see the results are now in the reverse order.

Step 3: Insert a Document into a Full Capped Collection

Let’s see what happens when we try to insert a document into a capped collection that has reached its capacity already using the following command.
db.student.insert({_id:10003,name:"Anba V M",age:16})
Here is the output,
> db.student.insert({_id:10003,name:"Anba V M",age:16})
WriteResult({ "nInserted" : 1 })
To query the student collection, use the following command.
> db.student.find()
{ "_id" : 10002, "name" : "Aruna M S", "age" : 14 }
{ "_id" : 10003, "name" : "Anba V M", "age" : 16 }
>

Notice that the first document is overwritten by the third document, as the maximum number of documents allowed in the student capped collection is two.

Create Operations

Create operations allow us to insert documents into a collection. These insert operations target a single collection. All write operations are atomic at the level of a single document.

MongoDB provides several methods for inserting documents, listed in Table 2-1.
Table 2-1

Insert Methods

db.collection.insertOne

Inserts a single document

db.collection.insertMany

Inserts multiple documents

db.collection.insert

Inserts a single document or multiple documents

Recipe 2-3. Insert Documents

In this recipe, we are going to discuss various methods to insert documents into a collection.

Problem

You want to insert documents into the collection person.

Solution

Use the following syntax to insert documents.
db.collection.insertOne()
db.collection.insertMany()

How It Works

Let’s follow the steps in this section to insert documents into the collection person.

Step 1: Insert a Single Document

To insert a single document into the collection person, use the following command.
db.person.insertOne({_id:1001,name:"Taanushree AS",age:10})
Here is the output,
> db.person.insertOne({_id:1001,name:"Taanushree AS",age:10})
{ "acknowledged" : true, "insertedId" : 1001 }
>

insertOne() returns a document that contains the newly inserted document’s _id.

If you don’t specify an _id field, MongoDB generates an _id field with an ObjectId value. The _id field act as the primary key.

Here is an example:
> db.person.insertOne({name:"Aruna MS",age:14})
{
        "acknowledged" : true,
        "insertedId" : ObjectId("5bac7a5113572c1fb994d2fe")
}
>

Step 2: Insert Multiple Documents

To insert multiple documents into the collection person , use the following command.
db.person.insertMany([{_id:1003,name:"Anba V M",age:16},{_id:1004,name:"shobana",age:44}])

Pass an array of documents to the insertMany() method to insert multiple documents.

Here is the output,
> db.person.insertMany([{_id:1003,name:"Anba V M",age:16},{_id:1004,name:"shobana",age:44}])
{ "acknowledged" : true, "insertedIds" : [ 1003, 1004 ] }
>

Read Operations

Read operations allow us to retrieve documents from a collection. MongoDB provides the find() method to query documents.

The syntax for the find() command is
db.collection.find()

You can specify query filters or criteria inside the find() method to query documents based on conditions.

Recipe 2-4. Query Documents

In this recipe, we are going to discuss how to retrieve documents from a collection.

Problem

You want to retrieve documents from a collection.

Solution

Use the following syntax to retrieve documents from a collection.
db.collection.find()

How It Works

Let’s follow the steps in this section to query documents in a collection.

Step 1: Select All Documents in a collection

To select all documents in a collection, use the following command.
db.person.find({})

You need to pass an empty document as a query parameter to the find() method.

Here is the output,
> db.person.find({})
{ "_id" : 1001, "name" : "Taanushree AS", "age" : 10 }
{ "_id" : ObjectId("5bac86dc773204ddade95819"), "name" : "Aruna MS", "age" : 14 }
{ "_id" : 1003, "name" : "Anba V M", "age" : 16 }
{ "_id" : 1004, "name" : "shobana", "age" : 44 }
>

Step 2: Specify Equality Conditions

To specify an equality condition, you need to use <field>:<value> expressions in the query to filter documents.

To select a document from the collection person where the name equals "shobana", here is the command:
db.person.find({name:"shobana"})
Here is the output,
> db.person.find({name:"shobana"})
{ "_id" : 1004, "name" : "shobana", "age" : 44 }
>

Step 3: Specify Conditions Using Query Operator

To select all documents from the collection person where age is greater than 10, here is the command:
db.person.find({age:{$gt:10}})
Here is the output,
> db.person.find({age:{$gt:10}})
{ "_id" : ObjectId("5bac86dc773204ddade95819"), "name" : "Aruna MS", "age" : 14 }
{ "_id" : 1003, "name" : "Anba V M", "age" : 16 }
{ "_id" : 1004, "name" : "shobana", "age" : 44 }
>

Step 4: Specify AND Conditions

In a compound query, you can specify conditions for more than one field. To select all documents in the person collection where the name equals "shobana" and the age is greater than 10, here is the command:
db.person.find({ name:"shobana",age:{$gt:10}})
Here is the output,
> db.person.find({ name:"shobana",age:{$gt:10}})
{ "_id" : 1004, "name" : "shobana", "age" : 44 }
>

Step 5: Specify OR Conditions

You can use $or in a compound query to join each clause with a logical or conjunction.

The operator $or selects the documents from the collection that match at least one of the selected conditions.

To select all documents in the person collection where the name equals "shobana" or age equals 20, here is the command:
db.person.find( { $or: [ { name: "shobana" }, { age: { $eq: 20 } } ] } )
Here is the output,
> db.person.find( { $or: [ { name: "shobana" }, { age: { $eq: 20 } } ] } )
{ "_id" : 1004, "name" : "shobana", "age" : 44 }
>

Update Operations

Update operations allow us to modify existing documents in a collection. MongoDB provides the update methods listed in Table 2-2.
Table 2-2

Update Methods

db.collection.updateOne

To modify a single document

db.collection.updateMany

To modify multiple documents

db.collection.replaceOne

To replace the first matching document in the collection that matches the filter

In MongoDB, update operations target a single collection.

Recipe 2-5. Update Documents

In this recipe, we are going to discuss how to update documents.

Problem

You want to update documents in a collection.

Solution

The following methods are used to update documents in a collection.
db.collection.updateOne()
db.collection.updateMany()
db.collection.replaceOne()
Execute the following code in the mongo shell to create the student collection.
db.student.insertMany([{_id:1001,name:"John",marks:{english:35,maths:38},result:"pass"},{_id:1002,name:"Jack",marks:{english:15,maths:18},result:"fail"},{_id:1003,name:"John",marks:{english:25,maths:28},result:"pass"},{_id:1004,name:"James",marks:{english:32,maths:40},result:"pass"},{_id:1005,name:"Joshi",marks:{english:15,maths:18},result:"fail"},{_id:1006,name:"Jack",marks:{english:35,maths:36},result:"pass"}])

How It Works

Let’s follow the steps in this section to update documents in a collection.

Step 1: Update a Single Document

MongoDB provides modify operators to modify field values such as $set.

To use the update operators, pass to the update methods an update document of the form:
{
  <update operator>: { <field1>: <value1>, ... },
  <update operator>: { <field2>: <value2>, ... },
  ...
}
To update the first document where name equals joshi, execute the following command.
db.student.updateOne({name: "Joshi"},{$set:{"marks.english": 20}})
Here is the output,
> db.student.updateOne({name: "Joshi"},{$set:{"marks.english": 20}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
>
Confirm that the field marks has been updated in the student collection:
> db.student.find({name:"Joshi"})
{ "_id" : 1005, "name" : "Joshi", "marks" : { "english" : 20, "maths" : 18 }, "result" : "fail" }
>

Note

You cannot update the _id field.

Step 2: Update Multiple Documents

Use the following code to update all documents in the student collection where result equals fail.
db.student.updateMany( { "result":"fail" }, { $set: { "marks.english": 20, marks.maths: 20 }  })
Here is the output,
> db.student.updateMany( { "result":"fail" }, { $set: {"marks.english": 20, "marks.maths": 20 }})
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
>

Here, the modifiedCount is 2, which indicates that the preceding command modified two documents in the student collection.

Step 3: Replace a Document

You can replace the entire content of a document except the _id field by passing an entirely new document as the second argument to db.collection.replaceOne() as shown here.

Execute the following command to replace the first document from the student collection where name equals "John".
db.student.replaceOne( { name: "John" }, {_id:1001,name:"John",marks:{english:36,maths:39},result:"pass"})
Here is the output,
> db.student.replaceOne( { name: "John" }, {_id:1001,name:"John",marks:{english:36,maths:39},result:"pass"})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
>

Note

Do not include update operators in the replacement document. You can omit the _id field in the replacement document because the _id field is immutable. However, if you want to include the _id field, use the same value as the current value.

Delete Operations

Delete operations allow us to delete documents from a collection. MongoDB provides two delete methods, as shown in Table 2-3.
Table 2-3

Delete Methods

db.collection.deleteOne

To delete a single document

db.collection.updateMany

To delete multiple documents

In MongoDB, delete operations target a single collection.

Recipe 2-6. Delete Documents

In this recipe, we discuss how to delete documents.

Problem

You want to delete documents from a collection.

Solution

The following methods are used to delete documents from a collection.
db.collection.deleteOne()
db.collection.deleteMany()

How It Works

Let’s follow the steps in this section to delete documents in a collection.

Step 1: Delete Only One Document That Matches a Condition

To delete a document from the collection student where name is John, use this code.
db.student.deleteOne({name: "John"})
Here is the output,
> db.student.deleteOne({name: "John"})
{ "acknowledged" : true, "deletedCount" : 1 }
>

Step 2: Delete All Documents That Match a Condition

To delete all documents from the collection student where name is Jack, use this code.
db.student.deleteMany({name: "Jack"})
Here is the output,
> db.student.deleteMany({name: "Jack"})
{ "acknowledged" : true, "deletedCount" : 2 }
>

Step 3: Delete All Documents from a Collection

To delete all documents from the collection student, pass an empty filter document to the db.student.deleteMany() method.
db.student.deleteMany({})
Here is the output,
> db.student.deleteMany({})
{ "acknowledged" : true, "deletedCount" : 3 }
>

MongoDB Import and Export

The MongoDB import tool allows us to import content from JSON, comma-separated value (CSV), and tab-separated value (TSV) files. MongoDB import only supports files that are UTF-8 encoded.

The MongoDB export tool allows us to export data stored in MongoDB instances as JSON or CSV files.

Recipe 2-7. Work with Mongo Import

In this recipe, we are going to discuss how to import data from a CSV file.

Problem

You want to import data from the student.csv file to the students collection.

Solution

The following command is used to perform a Mongo import.
mongoimport

Note

To work with the Mongo import command, start the mongod process and open another command prompt to issue the mongo import command.

How It Works

Let’s follow the steps in this section to work with Mongo import.

Create a student.csv file using the following data in the C:Sample directory.
_id,name,class
1,John,II
2,James,III
3,Joshi,I
Execute the following command to import data from the student.csv file to the students collection.
mongoimport --db student --collection students --type csv --headerline --file c:Samplestudent.csv
Here is the output,
C:Program FilesMongoDBServer4.0in>mongoimport --db student --collection students --type csv --headerline --file c:Samplestudent.csv
2018-09-28T07:00:20.909+0530    connected to: localhost
2018-09-28T07:00:20.969+0530    imported 3 documents
C:Program FilesMongoDBServer4.0in>
To confirm the existence of the students collection , issue the following commands.
> use student
switched to db student
> show collections
students
> db.students.find()
{ "_id" : 1, "name" : "John", "class" : "II" }
{ "_id" : 3, "name" : "Joshi", "class" : "I" }
{ "_id" : 2, "name" : "James", "class" : "III" }
>

Recipe 2-8. Work with Mongo Export

In this recipe, we are going to discuss how to export data from the students collection to a student.json file.

Problem

You want to export data from the students collection to a student.json file.

Solution

The following command is used to perform a Mongo export.
mongoexport

Note

To work with the Mongo export command, start the mongod process and open another command prompt to issue the Mongo export command.

How It Works

Let’s follow the steps in this section to work with Mongo export.

Execute the following command to export data from the students collection to a student.json file.
mongoexport --db student --collection students --out C:Samplestudent.json
Here is the output,
C:Program FilesMongoDBServer4.0in>mongoexport --db student --collection students --out C:Samplestudent.json
2018-09-28T07:11:19.446+0530    connected to: localhost
2018-09-28T07:11:19.459+0530    exported 3 records
The confirm the export, open the student.json file.
{"_id":1,"name":"John","class":"II"}
{"_id":2,"name":"James","class":"III"}
{"_id":3,"name":"Joshi","class":"I"}

Embedded Documents in MongoDB

Using embedded documents allows us to embed a document within another document. Consider this example.
{_id:1001,name:"John",marks:{english:35,maths:38},result:"pass"}

Here, the marks field contains an embedded document.

Execute the following code to create an employee database and employee collection.
use employee
db.employee.insertMany([{_id:1001,name:"John",address:{previous:"123,1st Main",current:"234,2nd Main"},unit:"Hadoop"},{_id:1002,name:"Jack", address:{previous:"Cresent Street",current:"234,Bald Hill Street"},unit:"MongoDB"},{_id:1003,name:"James", address:{previous:"Cresent Street",current:"234,Hill Street"},unit:"Spark"}])

Recipe 2-9. Query Embedded Documents

In this recipe, we are going to discuss how to query embedded documents.

Problem

You want to query embedded documents.

Solution

The following command is used to query embedded documents.
db.collection.find()

You need to pass the filter document {<field>:<value>} to the find() method where <value> is the document to match.

How It Works

Let’s follow the steps in this section to query embedded documents.

Step 1: Match an Embedded or Nested Document

To perform an equality match on the embedded document, you need to specify the exact match document in the <value> document, including the field order.

To select all documents where the address field equals the document {previous:"Cresent Street",current:"234,Bald Hill Street"}:
db.employee.find( { address: { previous:"Cresent Street",current:"234,Bald Hill Street" }} )
Here is the output,
> db.employee.find( { address: { previous:"Cresent Street",current:"234,Bald Hill Street" }} )
{ "_id" : 1002, "name" : "Jack", "address" : { "previous" : "Cresent Street", "current" : "234,Bald Hill Street" }, "unit" : "MongoDB" }
>

Step 2: Query on a Nested Field

We can use dot notation to query a field of the embedded document.

To select all documents where the previous field nested in the address field equals "Cresent Street",
db.employee.find( { "address.previous": "Cresent Street" } )
Here is the output,
> db.employee.find( { "address.previous": "Cresent Street" } )
{ "_id" : 1002, "name" : "Jack", "address" : { "previous" : "Cresent Street", "current" : "234,Bald Hill Street" }, "unit" : "MongoDB" }
{ "_id" : 1003, "name" : "James", "address" : { "previous" : "Cresent Street", "current" : "234,Hill Street" }, "unit" : "Spark" }
>

Working with Arrays

In MongoDB, we can specify field values as an array.

Recipe 2-10. Working with Arrays

In this recipe, we are going to discuss how to query an array.

Problem

You want to query an array.

Solution

The following command is used to query an array.
db.collection.find()

How It Works

Let’s follow the steps in this section to query embedded documents.

Step 1: Match an Array

Execute the code shown here to create the employeedetails collection under the employee database.
db.employeedetails.insertMany([
   { name: "John", projects: ["MongoDB", "Hadoop","Spark"], scores:[25,28,29] }, { name: "James", projects: [ "Cassandra","Spark"], scores:[26,24,23]}, { name: "Smith", projects: [ "Hadoop","MongoDB"], scores:[22,28,26]} ] )

To specify an equality condition on an array, you need to specify the exact array to match in the <value> of the query document {<field>:<value>}.

To select all documents where the projects value is an array with exactly two elements, "Hadoop" and "MongoDB", in the specified order, use the following commands.
db.employeedetails.find( { projects: ["Hadoop", "MongoDB"] } )
Here is the output,
> db.employeedetails.find( { projects: ["Hadoop", "MongoDB"] } )
{ "_id" : ObjectId("5badcbd5f10ab299920f072a"), "name" : "Smith", "projects" : [ "Hadoop", "MongoDB" ], "scores" : [ 22, 28, 26 ] }
>

Step 2: Query an Array for an Element

To query all documents where projects is an array that contains the string "MongoDB" as one of its elements, execute the following command.
db.employeedetails.find( { projects: "MongoDB" } )
Here is the output,
> db.employeedetails.find( { projects: "MongoDB" } )
{ "_id" : ObjectId("5badcbd5f10ab299920f0728"), "name" : "John", "projects" : [ "MongoDB", "Hadoop", "Spark" ], "scores" : [ 25, 28, 29 ] }
{ "_id" : ObjectId("5badcbd5f10ab299920f072a"), "name" : "Smith", "projects" : [ "Hadoop", "MongoDB" ], "scores" : [ 22, 28, 26 ] }
>

Step 3: Specify Query Operators

To query all documents where the scores field is an array that contains at least one element whose value is greater than 26 we can use the $gt operator.
db.employeedetails.find( { scores:{$gt:26} } )
Here is the output,
> db.employeedetails.find( { scores:{$gt:26} } )
{ "_id" : ObjectId("5badcbd5f10ab299920f0728"), "name" : "John", "projects" : [ "MongoDB", "Hadoop", "Spark" ], "scores" : [ 25, 28, 29 ] }
{ "_id" : ObjectId("5badcbd5f10ab299920f072a"), "name" : "Smith", "projects" : [ "Hadoop", "MongoDB" ], "scores" : [ 22, 28, 26 ] }
>

Step 4: Query an Array with Compound Filter Conditions on the Array Elements

A compound filter condition on the array can be specified as shown here.
db.employeedetails.find( { scores: { $gt: 20, $lt: 24 } } )
Here is the output,
> db.employeedetails.find( { scores: { $gt: 20, $lt: 24 } } )
{ "_id" : ObjectId("5badcbd5f10ab299920f0729"), "name" : "James", "projects" : [ "Cassandra", "Spark" ], "scores" : [ 26, 24, 23 ] }
{ "_id" : ObjectId("5badcbd5f10ab299920f072a"), "name" : "Smith", "projects" : [ "Hadoop", "MongoDB" ], "scores" : [ 22, 28, 26 ] }
>

Here, one element of the scores array can satisfy the greater than 20 condition and another element can satisfy the less than 24 condition, or a single element can satisfy both the conditions.

Step 5: Using the $elemMatch operator

The $elemMatch operator allows us to specify multiple conditions on the array elements such that at least one array element should satisfy all of the specified conditions.
db.employeedetails.find( { scores: { $elemMatch: { $gt: 23, $lt: 27 } } } )
Here is the output,
> db.employeedetails.find( { scores: { $elemMatch: { $gt: 23, $lt: 27 } } } )
{ "_id" : ObjectId("5badcbd5f10ab299920f0728"), "name" : "John", "projects" : [ "MongoDB", "Hadoop", "Spark" ], "scores" : [ 25, 28, 29 ] }
{ "_id" : ObjectId("5badcbd5f10ab299920f0729"), "name" : "James", "projects" : [ "Cassandra", "Spark" ], "scores" : [ 26, 24, 23 ] }
{ "_id" : ObjectId("5badcbd5f10ab299920f072a"), "name" : "Smith", "projects" : [ "Hadoop", "MongoDB" ], "scores" : [ 22, 28, 26 ] }
>

Step 6: Query an Array Element by Index Position

Use dot notation to query an array element by its index position.

Use this code to select all documents where the third element in the scores array is greater than 26.
db.employeedetails.find( { "scores.2": { $gt: 26 } } )
Here is the output,
> db.employeedetails.find( { "scores.2": { $gt: 26 } } )
{ "_id" : ObjectId("5badcbd5f10ab299920f0728"), "name" : "John", "projects" : [ "MongoDB", "Hadoop", "Spark" ], "scores" : [ 25, 28, 29 ] }
>

Step 7: Using the $size Operator

You can use the $size operator to query an array by number of elements.

To select all documents where the projects array has two elements, use this code.
db.employeedetails.find( { "projects": { $size: 2 } } )
Here is the output,
> db.employeedetails.find( { "projects": { $size: 2 } } )
{ "_id" : ObjectId("5badcbd5f10ab299920f0729"), "name" : "James", "projects" : [ "Cassandra", "Spark" ], "scores" : [ 26, 24, 23 ] }
{ "_id" : ObjectId("5badcbd5f10ab299920f072a"), "name" : "Smith", "projects" : [ "Hadoop", "MongoDB" ], "scores" : [ 22, 28, 26 ] }

Step 8: Using the $push Operator

You can use the $push operator to add a value to an array.

To add work location details, use this code.
db.employeedetails.update({name:"James"},{$push:{Location: "US"}})
Here is the query to check the result:
> db.employeedetails.find({name:"James"})
{ "_id" : ObjectId("5c04bef3540e90478dd92f4e"), "name" : "James", "projects" : [ "Cassandra", "Spark" ], "scores" : [ 26, 24, 23 ], "Location" : [ "US" ] }
To append multiple values to an array, use the $each operator.
db.employeedetails.update({name: "Smith"},{$push:{Location:{$each:["US","UK"]}}})
Here is the query to check the result:
> db.employeedetails.find({name:"Smith"})
{ "_id" : ObjectId("5c04bef3540e90478dd92f4f"), "name" : "Smith", "projects" : [ "Hadoop", "MongoDB" ], "scores" : [ 22, 28, 26 ], "Location" : [ "US", "UK" ] }
>

Step 9: Using the $addToSet Operator

You can use the $addToSet operator to add a value to an array. $addToSet adds a value to an array only if a value is not present. If a value is present, it does not do anything.

To add hobbies to the employeedetails collection, use this code.
db.employeedetails.update( {name: "James"},  { $addToSet: {hobbies: [ "drawing", "dancing"]} })
Here is the query to check the result:
> db.employeedetails.find({name:"James"})
{ "_id" : ObjectId("5c04bef3540e90478dd92f4e"), "name" : "James", "projects" : [ "Cassandra", "Spark" ], "scores" : [ 26, 24, 23 ], "Location" : [ "US" ], "hobbies" : [ [ "drawing", "dancing" ] ] }
>

Step 10: Using the $pop Operator

You can use the $pop operator to remove the first or last element of an array.

To remove the first element in the scores array, use this code.
db.employeedetails.update( {name: "James"},{ $pop: {scores:-1}})
Here is the query to check the result:
> db.employeedetails.find({name:"James"})
{ "_id" : ObjectId("5c04bef3540e90478dd92f4e"), "name" : "James", "projects" : [ "Cassandra", "Spark" ], "scores" : [ 24, 23 ], "Location" : [ "US" ], "hobbies" : [ [ "drawing", "dancing" ] ] }
>
To remove the last element in the scores array, use this code.
db.employeedetails.update( {name: "James"},{ $pop: {scores:1}})
Here is the query to check the result:
> db.employeedetails.find({name:"James"})
{ "_id" : ObjectId("5c04bef3540e90478dd92f4e"), "name" : "James", "projects" : [ "Cassandra", "Spark" ], "scores" : [ 24 ], "Location" : [ "US" ], "hobbies" : [ [ "drawing", "dancing" ] ] }
>

Recipe 2-11. Query an Array of Embedded Documents

In this recipe, we are going to discuss how to query an array of embedded documents.

Problem

You want to query an array of embedded documents.

Solution

The following command is used to query an array of embedded documents.
db.collection.find()

How It Works

Let’s follow the steps in this section to query an array of embedded documents.

Step 1: Query for a Document Nested in an Array

Execute the following code to create a student database and studentmarks collection.
> use student
> db.studentmarks.insertMany([{name:"John",marks:[{class: "II", total: 489},{ class: "III", total: 490 }]},{name: "James",marks:[{class: "III", total: 469 },{class: "IV", total: 450}]},{name:"Jack",marks:[{class:"II", total: 489 },{ class: "III", total: 390}]},{name:"Smith", marks:[{class: "III", total: 489}, {class: "IV", total: 490}]}, {name:"Joshi",marks:[{class: "II", total: 465}, { class: "III", total: 470}]}])
The command shown here selects all documents in the array field marks that match the specified document.
db.studentmarks.find( { "marks": {class: "II", total: 489}})
Here is the output,
> db.studentmarks.find( { "marks": {class: "II", total: 489}})
{ "_id" : ObjectId("5bae10e6f10ab299920f073f"), "name" : "John", "marks" : [ { "class" : "II", "total" : 489 }, { "class" : "III", "total" : 490 } ] }
{ "_id" : ObjectId("5bae10e6f10ab299920f0741"), "name" : "Jack", "marks" : [ { "class" : "II", "total" : 489 }, { "class" : "III", "total" : 390 } ] }
>

Note

Equality matching on the array of an embedded document requires an exact match of the specified document, including field order.

Step 2: Query for a Field Embedded in an Array of Documents

You can use dot notation to query a field embedded in an array of documents. The following command selects all documents in the marks array where at least one embedded document contains the field total that has a value that is less than 400.
db.studentmarks.find( { 'marks.total': { $lt: 400 } } )
Here is the output,
> db.studentmarks.find( { 'marks.total': { $lt: 400 } } )
{ "_id" : ObjectId("5bae10e6f10ab299920f0741"), "name" : "Jack", "marks" : [ { "class" : "II", "total" : 489 }, { "class" : "III", "total" : 390 } ] }
>

Step 3: Array Index to Query for a Field in the Embedded Document

The command shown here selects all the documents in the marks array where the first element of the document contains the field class, which has a value that is equal to "II".
db.studentmarks.find( { 'marks.0.class': "II" } )
Here is the output,
> db.studentmarks.find( { 'marks.0.class': "II" } )
{ "_id" : ObjectId("5bae10e6f10ab299920f073f"), "name" : "John", "marks" : [ { "class" : "II", "total" : 489 }, { "class" : "III", "total" : 490 } ] }
{ "_id" : ObjectId("5bae10e6f10ab299920f0741"), "name" : "Jack", "marks" : [ { "class" : "II", "total" : 489 }, { "class" : "III", "total" : 390 } ] }
{ "_id" : ObjectId("5bae10e6f10ab299920f0743"), "name" : "Joshi", "marks" : [ { "class" : "II", "total" : 465 }, { "class" : "III", "total" : 470 } ] }
>

Project Fields to Return from Query

By default, queries in MongoDB return all fields in matching documents. You can restrict the fields to be returned using a projection document.

Execute the following code to create a studentdetails collection under the student database.
db.studentdetails.insertMany( [ {name: "John", result: "pass", marks: { english: 25, maths: 23, science: 25 }, grade: [ { class: "A", total: 73 } ] },{name: "James", result: "pass", marks: { english: 24, maths: 25, science: 25 }, grade: [ { class: "A", total: 74 } ] },{name: "James", result: "fail", marks: { english: 12, maths: 13, science: 15 }, grade: [ { class: "C", total: 40 } ] }])

Recipe 2-12. Restricting the Fields Returned from a Query

In this recipe, we are going to discuss how to restrict the fields to return from a query.

Problem

You want to restrict the fields to return from a query.

Solution

The following command is used to restrict the fields to return from a query.
db.collection.find()

How It Works

Let’s follow the steps in this section to restrict the fields to return from a query.

Step 1: Return the Specified Fields and the _id Field Only

You can project the required fields by setting the <field> value to 1 in the projection document.

The command shown here returns only the _id, marks, and result fields where name equals "John" in the result set.
db.studentdetails.find( { name: "John" }, { marks: 1, result: 1 } )
Here is the output,
> db.studentdetails.find( { name: "John" }, { marks: 1, result: 1 } )
{ "_id" : ObjectId("5bae3ed2f10ab299920f0744"), "result" : "pass", "marks" : { "english" : 25, "maths" : 23, "science" : 25 } }
>

Step 2: Suppress the _id Field

You can suppress the _id field by setting its exclusion <field> to 0 in the projection document.

The following command suppresses the _id field in the result set.
db.studentdetails.find( { name: "John" }, { marks: 1, result: 1,_id:0 } )
Here is the output,
> db.studentdetails.find( { name: "John" }, { marks: 1, result: 1,_id:0 } )
{ "result" : "pass", "marks" : { "english" : 25, "maths" : 23, "science" : 25 } }
>

Step 3: Exclude More Than One Field

You can exclude fields by setting <field> to 0 in the projection document.

The following command suppresses the _id and result fields in the result set.
db.studentdetails.find( { name: "John" }, { result:0,_id:0 } )
Here is the output,
> db.studentdetails.find( { name: "John" }, { result:0,_id:0 } )
{ "name" : "John", "marks" : { "english" : 25, "maths" : 23, "science" : 25 }, "grade" : [ { "class" : "A", "total" : 73 } ] }
>

Step 4: Return a Specific Field in an Embedded Document

You can use dot notation to refer to the embedded field and set the <field> to 1 in the projection document.

Here is an example:
db.studentdetails.find( { name: "John }, { result:1, grade: 1, "marks.english": 1 })
Here is the output,
> db.studentdetails.find( { name: "John" }, { result:1, grade: 1, "marks.english": 1 })
{ "_id" : ObjectId("5bae3ed2f10ab299920f0744"), "result" : "pass", "marks" : { "english" : 25 }, "grade" : [ { "class" : "A", "total" : 73 } ] }
>

Query for Null or Missing Fields

Execute the following code to create the sample collection.
db.sample.insertMany([ { _id: 1, name: null }, { _id: 2 }])

Recipe 2-13. To Query Null or Missing Fields

In this recipe, we are going to discuss how to query null or missing values in MongoDB.

Problem

You want to query null or missing values.

Solution

The following command is used to query null or missing values.
db.collection.find()

How It Works

Let’s follow the steps in this section to query null or missing values.

Step 1: Equality Filter

The {name:null} query matches documents that either contain the name field with a value of null or do not contain the name field.
db.sample.find( { name: null } )
Here is the output,
> db.sample.insertMany([{ _id: 1, name: null }, { _id: 2 }])
{ "acknowledged" : true, "insertedIds" : [ 1, 2 ] }
>

Here, it returns both the documents.

Step 2: Type Check

The query shown here returns the documents that only contain the name field with a value of null. In BSON, set the null value to 10.
db.sample.find( { name: { $type: 10 } } )
Here is the output,
> db.sample.find( { name: { $type: 10 } } )
{ "_id" : 1, "name" : null }
>

Step 3: Existence Check

You can use the $exist operator to check for the existence of a field.

The following query returns the documents that do not contain the name field.
db.sample.find( { name: { $exists: false } } )
Here is the output,
> db.sample.find( { name: { $exists: false } } )
{ "_id" : 2 }
>

Iterate a Cursor

The db.collection.find() method of MongoDB returns a cursor. You need to iterate a cursor to access the documents. You can manually iterate a cursor in the mongo shell by assigning a cursor returned from the find() method to a variable using the var keyword.

If you are not assigning a cursor to a variable using the var keyword, it is automatically iterated up to 20 times to print the first 20 documents.

Recipe 2-14. Iterate a Cursor

In this recipe, we are going to discuss how to iterate a cursor in the mongo shell.

Problem

You want to iterate a cursor in the mongo shell.

Solution

The following syntax is used to iterate a cursor.
var myCursor = db.collection.find()
myCursor

How It Works

Let’s follow the steps in this section to iterate a cursor. First, create a numbers collection.
db.numbers.insert({_id:1,number:1});
db.numbers.insert({_id:2,number:2});
db.numbers.insert({_id:3,number:3});
db.numbers.insert({_id:4,number:4});
db.numbers.insert({_id:5,number:5});
db.numbers.insert({_id:6,number:6});
db.numbers.insert({_id:7,number:7});
db.numbers.insert({_id:8,number:8});
db.numbers.insert({_id:9,number:9});
db.numbers.insert({_id:10,number:10});
db.numbers.insert({_id:11,number:11});
db.numbers.insert({_id:12,number:12});
db.numbers.insert({_id:13,number:13});
db.numbers.insert({_id:14,number:14});
db.numbers.insert({_id:15,number:15});
db.numbers.insert({_id:16,number:16});
db.numbers.insert({_id:17,number:17});
db.numbers.insert({_id:18,number:18});
db.numbers.insert({_id:19,number:19});
db.numbers.insert({_id:20,number:20});
db.numbers.insert({_id:21,number:21});
db.numbers.insert({_id:22,number:22});
db.numbers.insert({_id:23,number:23});
db.numbers.insert({_id:24,number:24});
db.numbers.insert({_id:25,number:25});
Issue the following commands to iterate a cursor.
var myCursor = db.numbers.find()
myCursor
Here is the output,
> var myCursor = db.numbers.find()
> myCursor
{ "_id" : 1, "number" : 1 }
{ "_id" : 2, "number" : 2 }
{ "_id" : 3, "number" : 3 }
{ "_id" : 4, "number" : 4 }
{ "_id" : 5, "number" : 5 }
{ "_id" : 6, "number" : 6 }
{ "_id" : 7, "number" : 7 }
{ "_id" : 8, "number" : 8 }
{ "_id" : 9, "number" : 9 }
{ "_id" : 10, "number" : 10 }
{ "_id" : 11, "number" : 11 }
{ "_id" : 12, "number" : 12 }
{ "_id" : 13, "number" : 13 }
{ "_id" : 14, "number" : 14 }
{ "_id" : 15, "number" : 15 }
{ "_id" : 16, "number" : 16 }
{ "_id" : 17, "number" : 17 }
{ "_id" : 18, "number" : 18 }
{ "_id" : 19, "number" : 19 }
{ "_id" : 20, "number" : 20 }
Type "it" for more
> it
{ "_id" : 21, "number" : 21 }
{ "_id" : 22, "number" : 22 }
{ "_id" : 23, "number" : 23 }
{ "_id" : 24, "number" : 24 }
{ "_id" : 25, "number" : 25 }
>
You can also use the cursor method next() to access the document.
var myCursor=db.numbers.find({});
while(myCursor.hasNext()){
  print(tojson(myCursor.next()));
}
Here is the output,
> var myCursor=db.numbers.find({});
> while(myCursor.hasNext()){
... print(tojson(myCursor.next()));
... }
{ "_id" : 1, "number" : 1 }
{ "_id" : 2, "number" : 2 }
{ "_id" : 3, "number" : 3 }
{ "_id" : 4, "number" : 4 }
{ "_id" : 5, "number" : 5 }
{ "_id" : 6, "number" : 6 }
{ "_id" : 7, "number" : 7 }
{ "_id" : 8, "number" : 8 }
{ "_id" : 9, "number" : 9 }
{ "_id" : 10, "number" : 10 }
{ "_id" : 11, "number" : 11 }
{ "_id" : 12, "number" : 12 }
{ "_id" : 13, "number" : 13 }
{ "_id" : 14, "number" : 14 }
{ "_id" : 15, "number" : 15 }
{ "_id" : 16, "number" : 16 }
{ "_id" : 17, "number" : 17 }
{ "_id" : 18, "number" : 18 }
{ "_id" : 19, "number" : 19 }
{ "_id" : 20, "number" : 20 }
{ "_id" : 21, "number" : 21 }
{ "_id" : 22, "number" : 22 }
{ "_id" : 23, "number" : 23 }
{ "_id" : 24, "number" : 24 }
{ "_id" : 25, "number" : 25 }
>
You can also use forEach to iterate the cursor and access the document.
var myCursor = db.numbers.find()
myCursor.forEach(printjson);
> var myCursor = db.numbers.find()
> myCursor.forEach(printjson);
{ "_id" : 1, "number" : 1 }
{ "_id" : 2, "number" : 2 }
{ "_id" : 3, "number" : 3 }
{ "_id" : 4, "number" : 4 }
{ "_id" : 5, "number" : 5 }
{ "_id" : 6, "number" : 6 }
{ "_id" : 7, "number" : 7 }
{ "_id" : 8, "number" : 8 }
{ "_id" : 9, "number" : 9 }
{ "_id" : 10, "number" : 10 }
{ "_id" : 11, "number" : 11 }
{ "_id" : 12, "number" : 12 }
{ "_id" : 13, "number" : 13 }
{ "_id" : 14, "number" : 14 }
{ "_id" : 15, "number" : 15 }
{ "_id" : 16, "number" : 16 }
{ "_id" : 17, "number" : 17 }
{ "_id" : 18, "number" : 18 }
{ "_id" : 19, "number" : 19 }
{ "_id" : 20, "number" : 20 }
{ "_id" : 21, "number" : 21 }
{ "_id" : 22, "number" : 22 }
{ "_id" : 23, "number" : 23 }
{ "_id" : 24, "number" : 24 }
{ "_id" : 25, "number" : 25 }
>

Working with the limit() and skip() Methods

The limit() method is used to limit the number of documents in the query results and the skip() method skips the given number of documents in the query result.

Recipe 2-15. limit() and skip() Methods

In this recipe, we are going to discuss how to work with the limit() and skip() methods .

Problem

You want to limit and skip the documents in a collection.

Solution

The following syntax is used to limit the number of documents in the query result.
db.collection.find().limit(2)
The following syntax is used to skip a given number of documents in the query result.
db.collection.find().skip(2)

How It Works

Let’s follow the steps in this section to work with the limit() and skip() methods. Consider the numbers collection created in Recipe 2-14.

To display the first two documents, use this code.
db.numbers.find().limit(2)
Here is the output,
> db.numbers.find().limit(2)
{ "_id" : 1, "number" : 1 }
{ "_id" : 2, "number" : 2 }
>
To skip the first five documents, use this code.
db.numbers.find().skip(5)
Here is the output,
> db.numbers.find().skip(5)
{ "_id" : 6, "number" : 6 }
{ "_id" : 7, "number" : 7 }
{ "_id" : 8, "number" : 8 }
{ "_id" : 9, "number" : 9 }
{ "_id" : 10, "number" : 10 }
{ "_id" : 11, "number" : 11 }
{ "_id" : 12, "number" : 12 }
{ "_id" : 13, "number" : 13 }
{ "_id" : 14, "number" : 14 }
{ "_id" : 15, "number" : 15 }
{ "_id" : 16, "number" : 16 }
{ "_id" : 17, "number" : 17 }
{ "_id" : 18, "number" : 18 }
{ "_id" : 19, "number" : 19 }
{ "_id" : 20, "number" : 20 }
{ "_id" : 21, "number" : 21 }
{ "_id" : 22, "number" : 22 }
{ "_id" : 23, "number" : 23 }
{ "_id" : 24, "number" : 24 }
{ "_id" : 25, "number" : 25 }
>

Working with Node.js and MongoDB

MongoDB is one of the most popular databases used with Node.js .

Recipe 2-16. Node.js and MongoDB

In this recipe, we are going to discuss how to work with Node.js and MongoDB.

Problem

You want to perform CRUD operations using Node.js.

Solution

Download the Node.js installer from https://nodejs.org/en/download/ and install it.

Note

The given link might be changed in the future.

Next, open the command prompt and issue the below command to install mongodb database driver.
npm install mongodb

How It Works

Let’s follow the steps in this section to work with Node.js and MongoDB.

Step 1: Establishing a Connection and Creating a Collection

Save the following code in a file named connect.js.
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url,{useNewUrlParser:true,useUnifiedTopology:true},function(err, db) {
  if (err) throw err;
  var mydb = db.db("mydb");
  mydb.createCollection("employees", function(err, res) {
    if (err) throw err;
    console.log("Collection created!");
    db.close();
  });
});
Run the code by issuing this command at the terminal.
node connect.js
Here is the output,
C:UsersDHARANIDesktop>node connect.js
Collection created!
../images/475461_1_En_2_Chapter/475461_1_En_2_Figa_HTML.jpg

Step 2: Insert a Document

Save the code shown here in a file called insert.js.
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, {useNewUrlParser:true, useUnifiedTopology:true},function(err, db) {
  if (err) throw err;
  var mydb = db.db("mydb");
  var myobj = { name: "Subhashini", address: "E 603 Alpyne" };
  mydb.collection("employees").insertOne(myobj, function(err, res) {
    if (err) throw err;
    console.log("Inserted");
    db.close();
  });
});
Run the code by issuing the following command at the command prompt.
node insert.js
Here is the output,
> show collections
mydb
> db.employees.find()
{ "_id" : ObjectId("5c050dd8c63a253134cbd375"), "name" : "Subhashini", "address" : "E 603 Alpyne" }
>
To insert multiple documents, use this command.
var myobj = [{name:"Shobana",address: "E 608 Alpyne"},{name:"Taanu", address:"Valley Street"}];

Step 3: Query a Document

Save the following code in a file called find.js.
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, {useNewUrlParser:true, useUnifiedTopology:true}, function(err, db) {
  if (err) throw err;
  var mydb = db.db("mydb");
  var query = {name:"Subhashini" };
  mydb.collection("employees").find(query).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
    db.close();
  });
});
Run the code by issuing this command at the command prompt.
node find.js
Here is the output,
 [ { _id: 5c050dd8c63a253134cbd375,
    name: 'Subhashini',
    address: 'E 603 Alpyne' } ]

Step 4: Update a Document

Save the code shown here in a file called update.js.
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://127.0.0.1:27017/";
MongoClient.connect(url, {useNewUrlParser:true, useUnifiedTopology:true}, function(err, db) {
  if (err) throw err;
  var mydb = db.db("mydb");
  var query = {address:"E 603 Alpyne" };
  var newvalues = { $set: {address:"New Street"}};
  mydb.collection("employees").updateOne(query, newvalues, function(err, res) {
    if (err) throw err;
    console.log("Document Updated");
    db.close();
  });
});
Run the code by issuing the following command at the command prompt.
node update.js
Here is the output,
> db.employees.find({name:"Subhashini"})
{ "_id" : ObjectId("5c050dd8c63a253134cbd375"), "name" : "Subhashini", "address" : "New Street" }
>

Step 5: Delete a Document

Save the code shown here in a file called delete.js.
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, {useNewUrlParser:true, useUnifiedTopology:true}, function(err, db) {
  if (err) throw err;
  var mydb = db.db("mydb");
  var query = {name:"Subhashini"};
  mydb.collection("employees").deleteOne(query, function(err, obj) {
    if (err) throw err;
    console.log("Document deleted");
    db.close();
  });
});
Run the code by issuing the following command at the command prompt.
node delete.js
Here is the output,
Document deleted
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.44.53