DSS - U3 - Chap6 - MongoDB Rev 1.1
DSS - U3 - Chap6 - MongoDB Rev 1.1
Introduction to MongoDB
New kind of database is gaining ground in the enterprise called NoSQL (Not only SQL). We explore a NoSQL
database called "MongoDB".
WHAT IS MONGODB?
MongoDB is
1. Cross-platform.
2. Open source.
3. Non-relational.
4. Distributed.
5. NoSQL.
6. Document-oriented data store.
WHY MONGODB?
- Deal with large volumes of data,
- Rich variety of data-particularly unstructured data
- Scaling of enterprise data - database that can scale out or scale horizontally to meet the scale requirements
- Flexibile schema
- Fault tolerant,
- Consistent and Partition tolerant (CAP theorem or Brewers theorem)
- Easily distributed over a multitude of nodes in a cluster.
Capped collections are fixed-size collections that support high-throughput operations that insert and
retrieve documents based on insertion order
JSON is extremely expressive. MongoDB actually does not use JSON but BSON (pronounced Bee Son) - it is
Binary JSON. It is an open standard, it is used to store complex data structures.
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #2
Let us look CSV file having data is about the employees of “XYZ” organization.. Here we can see, that
column values are separated using commas and the rows are separated by a carriage return.
This looks good! However let us make it slightly more legible by adding column heading.
Suppose if few employees have more than one ContactNo. It can be neatly classified as OfficeContactNo and
HomeContactNo. Or
if few employees have more than one Office Contact No and more than one Home Contact No?
Another example
In case of employees where we need to store their email addresses as well.
Similarily we have the same issues, few employees have two email addresses, few have three and there are a
few employees with more than three email addresses as well.
Here CSV are known to store data well if it is flat and does not have repeating values.
This problem can be solved by XML. But as the name suggests XML is highly extensible. It does not just
call for defining a data format, rather it defines how you define a data format. You may be prepared to
undertake this cumbersome task for highly complex and structured data; however, for simple data exchange it
might just be too much work.
As you can see it is quite easy to read a JSON. There is absolutely no confusion now. One can have a list of
contact numbers, and they can be stored with ease.
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #3
JSON is very expressive. JSON provide easy to store and retrieve documents in their real form.
BSON is an open standard, it consumes less space as compared to the text-based JSON.
Major advantage with BSON, It is easier and quicker to convert BSON to a programming language's
native data format.
MongoDB drivers available for a number of programming languages such as C, C++, Ruby, PHP, Python,
C#, etc., and each works slightly differently.
Using the basic binary format enables the native data structures to be built quickly for each language
without going through the hassle of first processing JSON.
Each JSON document should have a unique identifier. It is the _id key. It is similar to the primary key in
relational databases. This facilitates search for documents based on the unique identifier. An index is
automatically built on the unique identifier. It is your choice to either provide unique values yourself or have
the mongo shell generate the same.
Database
It is a collection of collections. In other words, it is like a container for collections. Data base is created first
time that your collection makes a reference to it. Database can be created on demand. Each database gets
its own set of files on the file system. A single MongoDB server can house several databases.
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #4
Collection
A collection is analogous to a table of RDBMS. A collection is created on demand.
Collection is created the first time that you attempt to save a document that references it.
A collection exists within a single database. A collection holds several MongoDB documents.
A collection does not enforce a schema. This implies that documents within a collection can have different
fields.
Even if the documents within a collection have same fields, the order of the fields can be different.
Document
A document is analogous to a row/record/tuple in an RDBMS table.
A document has a dynamic schema. This implies that a document in a collection need not necessarily have
the same set of fields/key-value pairs.
MongoDB provides GridFS to support the storage of binary data. This usually suffices for photographs
(such as a profile picture) or small audio clips. However, if one wishes to store movie clips, MongoDB has
another solution.
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #5
It stores the fs.files (metadata) (data about data along with the context information) in a collection called "file".
It then breaks the data into small pieces called fs.chunks(chunks) and stores it in the "chunks" collection.
This process takes care about the need for easy scalability.
• In some situations, storing large files may be more efficient in a MongoDB database than on a
system-level filesystem.
• If your filesystem limits the number of files in a directory, you can use GridFS to store as many files
as needed.
• When you want to access information from portions of large files without having to load whole files
into memory, you can use GridFS to recall sections of files without reading the entire file into
memory.
• When you want to keep your files and metadata automatically synced and deployed across a
number of systems and facilities, you can use GridFS. When using geographically distributed
replica sets, MongoDB can distribute files and their metadata automatically to a number of
mongod instances and facilities.
6.2.5 Replication
MongoDB provides data redundancy and high availability.
Replication provides redundancy and increases data availability. With multiple copies of data on different
database servers, replication provides a level of fault tolerance against the loss of a single database server.
In some cases, replication can provide increased read capacity as clients can send read operations to
different servers. Maintaining copies of data in different data centers can increase data locality and
availability for distributed applications. It helps to recover from hardware failure and service interruptions
You can also maintain additional copies for dedicated purposes, such as disaster recovery, reporting, or backup.
A replica set is a group of mongod instances that maintain the same data set. A replica set contains
several data bearing nodes and optionally one arbiter node. In MongoDB, the replica set has a single
primary and several secondaries.
Each write request from the client is directed to the primary. The primary logs all write requests into its
Oplog (operations log). The Oplog is then used by the secondary replica members to synchronize their
data. This way there is strict adherence to consistency.
In the above, The clients usually read from the primary. However, the client can also specify a read
preference that will then direct the read operations to the secondary.
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #6
6.2.6 Sharding
Sharding is akin to horizontal scaling. It means that the large data set is divided and distributed over
multiple servers or shards. Each shard is an independent database and collectively they would constitute a
logical database.
MongoDB updates the information in-place. This implies that it updates the data wherever it is available. It
does not allocate separate space and the indexes remain unaltered.
MongoDB is all for lazy-writes. It writes to the disk once every second.
Reading and writing to disk is a slow operation as compared to reading and writing from memory.
The fewer the reads and writes that we perform to the disk, the better is the performance.
This makes MongoDB faster than its other competitors who write almost immediately to the disk.
However, there is a tradeoff. MongoDB makes no guarantee that data will be stored safely on the disk.
To check your database exists or not , type the command at the MongoDB shell: db
run db in the shell to tell you the name of the active database
> db;
myDB
>
To get a list of all databases that already exist in the system, - show dbs
> show dbs;
admin (empty)
local 0.078GB
test 0.078GB
>
Note: Newly created database, "myDB" does not show up in the list above. In order to list out the database
needs to have at least one document in the list.
The default database in MongoDB is test. If one does not create any database, all collections are by default
stored in the test database.
6.3.2 Drop Database
syntax: db.dropDatabase();
e.g - To drop the database, "myDB", first ensure that you are currently placed in "myDB" database and then use
the db.dropDatabase() command to drop the database.
use myDB;
db.dropDatabase();
Confirm if the database "myDB" has been dropped.
>db.dropDatabase();
{ "dropped" : "myDB", "ok" : 1 }
>
Note: If no database is selected, the default database "test" is dropped.
>db.createCollection(“mongodb”, db.createCollection(“mongodb”);
{ capped:true, size:1000000, max:2}) { “ok” : 1 }
{ “ok” : 1 }
Here we can create collection with options as well as with out options
To display the list of collections (tables) in the current database
> show collections
system.indexes
system.js
>
To display the current version of the MongoDB server:
> db.version()
3.4.3
>
To display the statistics that reflect the use state of a database:
> db.stats() > db.stats()
{ {
"db" : "myDB1", "db" : "mydb",
"collections" : 3, "collections" : 1,
"objects" : 6, "views" : 0,
"avgObjSize" : 122.66666666667, "objects" : 1,
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #11
db.printReplicationInfo()
db.printShardi ngStatus()
db.printslaveReplicationInfo()
db.dropuser(username)
db.repairDatabase()
db.resetError()
db.runCommand(cmdobj) run a database command, if cmdobj is a string, turns it into { cmdobj : 1 }
db.serverStatus()
db.setProfilingLevel(level,<slowms>) 0=off l^slow 2=all
db.setwriteConcern( <write concern doc> ) - sets the write concern for w rites to the db
db.unsetWriteConcern( <write concern doc> ) - unsets the write concern f or writes to the db
db.setVerboseShell(flag) display extra information in shell output
db.shutdownServer()
db.stats()
db.version() current version of the server
****
Consider a table "Students" with the following columns:
1. StudRollNo 2. StudName 3. Grade 4. Hobbies 5. DOJ
Before we get into the details of CRUD operations in MongoDB, let us look at how the statements are written
in RDBMS and MongoDB.
Read —> Reading the data is performed using the find() method.
Update —> Update to data is accomplished using the update() method with UPSERT set to false.
Objective: To create a collection by the name "Person". Let us take a look at the collection list prior to the
creation of the new collection "Person":
> show collections
Students
food
Objective: To drop a collection by the name "food". Take a look at the current collection list:
Person
Students
food
> db.createCollection("student");
{ "ok" : 1 }
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #15
To Store the document(s) in to collection we had three functions 1.insert() 2.update() 3. save()
1. insert() - insert a fresh document in to collection
2. update() - insert a fresh document in to collection if document was not found with upsert:true option
if document is found we can update the document fields
3. save() - insert a fresh document in to collection if document was not found with _id
if document _id is found we can replace the document
1. insert() - insert a fresh document in to collection student
> db.student.insert( { "rollno":17301, "name":"Murali", "age":23, "contactno":9848929299,"e-
mail":"muralivadlamudi@gmail.com"})
WriteResult({ "nInserted" : 1 })
> db.student.find()
{ "_id" : ObjectId("5d65d54557c4c2d917f18bb3"), "rollno" : 17301, "name" : "Murali", "age" : 23, "contactno"
: 9848929299, "e-mail" : "muralivadlamudi@gmail.com" }
{ "_id" : ObjectId("5d65d89457c4c2d917f18bb4"), "rollno" : 17302, "name" : "Krishna", "age" : 23,
"contactno" : 9848111299, "e-mail" : "krishna@gmail.com" }
> db.student.find().pretty()
{
"_id" : ObjectId("5d65d54557c4c2d917f18bb3"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848929299,
"e-mail" : "muralivadlamudi@gmail.com"
}
{
"_id" : ObjectId("5d65d89457c4c2d917f18bb4"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848111299,
"e-mail" : "krishna@gmail.com"
}
>
2. update() - insert a fresh document in to collection if document was not found with upsert1:true option
if document is found we can update the document fields
Here we are searching for a document, if that is found we update it else we insert the document into
collection
> db.student.update({"rollno":17303,"name":"Vamsi","age":23},{$set:{"contactno":9848811299,"e-
mail":"vamsi@gmail.com"}},{upsert:true});
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("5d65de78386852e892928055")
})
upsert – is a boolean and Optional. If set to true, creates a new document when no document matches the
query criteria. The default value is false, which does not insert a new document when no match is found.
> db.student.find().pretty()
{
"_id" : ObjectId("5d65dd2ade1660c49412a60d"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848912299,
"e-mail" : "muralivadlamudi@gmail.com"
}
{
"_id" : ObjectId("5d65dd58de1660c49412a60e"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
1 upsert(Noun) An operation that inserts rows into a database table (if they do not already exist) or updates them
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #17
"contactno" : 9848191299,
"e-mail" : "krishna@gmail.com"
}
{
"_id" : ObjectId("5d65de78386852e892928055"),
"age" : 23,
"name" : "Vamsi",
"rollno" : 17303,
"contactno" : 9848811299,
"e-mail" : "vamsi@gmail.com"
}
>
In this example Name: Vamsi is not found hence record is inserted
Now we try to update Vamsi phone number to 7988811299 as vamsi document already exists
> db.student.update({"rollno":17303,"name":"Vamsi","age":23},{$set:{"contactno":7988811299}},
{upsert:true});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find().pretty()
{
"_id" : ObjectId("5d65dd2ade1660c49412a60d"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848912299,
"e-mail" : "muralivadlamudi@gmail.com"
}
{
"_id" : ObjectId("5d65dd58de1660c49412a60e"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848191299,
"e-mail" : "krishna@gmail.com"
}
{
"_id" : ObjectId("5d65de78386852e892928055"),
"age" : 23,
"name" : "Vamsi",
"rollno" : 17303,
"contactno" : 7988811299,
"e-mail" : "vamsi@gmail.com"
}
>
3. save() - insert a fresh document in to collection if document was not found with _id
if document _id is found we can replace the document
>db.student.save({"rollno":17304,"name":"Sudha","age":21,"contactno":6398193339,"e-
mail":"sudha@gmail.com"})
WriteResult({ "nInserted" : 1 })
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #18
> db.student.find().pretty()
{
"_id" : ObjectId("5d65dd2ade1660c49412a60d"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848912299,
"e-mail" : "muralivadlamudi@gmail.com"
}
{
"_id" : ObjectId("5d65dd58de1660c49412a60e"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848191299,
"e-mail" : "krishna@gmail.com"
}
{
"_id" : ObjectId("5d65de78386852e892928055"),
"age" : 23,
"name" : "Vamsi",
"rollno" : 17303,
"contactno" : 7988811299,
"e-mail" : "vamsi@gmail.com"
}
{
"_id" : ObjectId("5d65e206de1660c49412a60f"),
"rollno" : 17304,
"name" : "Sudha",
"age" : 21,
"contactno" : 6398193339,
"e-mail" : "sudha@gmail.com"
}
>
Search for the documents - find() (Search for the document based on criteria)
e.g – Search for document in collection student with rollno- 17302
>db.student.find({rollno:17302});
{ "_id" : ObjectId("5d65dd58de1660c49412a60e"), "rollno" : 17302, "name" : "Krishna", "age" : 23,
"contactno" : 9848191299, "e-mail" : "krishna@gmail.com" }
> db.student.find({rollno:17302}).pretty()
{
"_id" : ObjectId("5d65dd58de1660c49412a60e"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848191299,
"e-mail" : "krishna@gmail.com"
}
>
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #19
> db.student.find({"name":"sudha"}).pretty()
> db.student.find({"name":"Sudha"}).pretty()
{
"_id" : ObjectId("5d65e206de1660c49412a60f"),
"rollno" : 17304,
"name" : "Sudha",
"age" : 21,
"contactno" : 6398193339,
"e-mail" : "sudha@gmail.com"
}
>
Search for the document with out criteria projecting the specified fields
Now list out the specified fields in the document as per our choice
selecting rollno,name,contactno only
syntax : db.student.find({},{rollno:1,name:1,contactno:1}).pretty()
Here we set rollno:1,name:1,contactno:1
e.g
> db.student.find({},{rollno:1,name:1,contactno:1}).pretty()
{
"_id" : ObjectId("5d65dd2ade1660c49412a60d"),
"rollno" : 17301,
"name" : "Murali",
"contactno" : 9848912299
}
{
"_id" : ObjectId("5d65dd58de1660c49412a60e"),
"rollno" : 17302,
"name" : "Krishna",
"contactno" : 9848191299
}
{
"_id" : ObjectId("5d65de78386852e892928055"),
"name" : "Vamsi",
"rollno" : 17303,
"contactno" : 7988811299
}
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #20
{
"_id" : ObjectId("5d65e206de1660c49412a60f"),
"rollno" : 17304,
"name" : "Sudha",
"contactno" : 6398193339
}
> db.student.find({},{rollno:1,name:1,contactno:1,_id:0}).pretty()
{ "rollno" : 17301, "name" : "Murali", "contactno" : 9848912299 }
{ "rollno" : 17302, "name" : "Krishna", "contactno" : 9848191299 }
{ "name" : "Vamsi", "rollno" : 17303, "contactno" : 7988811299 }
{ "rollno" : 17304, "name" : "Sudha", "contactno" : 6398193339 }
>
Relational Operators
Name Description
$eq Matches values that are equal to a specified value.
$gt Matches values that are greater than a specified value.
$gte Matches values that are greater than or equal to a specified value.
$lt Matches values that are less than a specified value.
$lte Matches values that are less than or equal to a specified value.
$ne Matches all values that are not equal to a specified value.
$in Matches any of the values specified in an array. (similar to unix shell - for variable in list)
$nin Matches none of the values specified in an array. ( not in list)
In the above example we use empty criteria {}, as we need add field all records
> db.student.find().pretty()
{
"_id" : ObjectId("5d671076ae0af543597ceb16"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848912299,
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #21
"e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A"
}
{
"_id" : ObjectId("5d6710cfae0af543597ceb17"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848191299,
"e-mail" : "krishna@gmail.com",
"creditscore" : "A"
}
{
"_id" : ObjectId("5d67112bae0af543597ceb18"),
"rollno" : 17303,
"name" : "Vamsi",
"age" : 22,
"contactno" : 7988811299,
"e-mail" : "vamsi@gmail.com",
"creditscore" : "A"
}
{
"_id" : ObjectId("5d671175ae0af543597ceb19"),
"rollno" : 17304,
"name" : "Sudha",
"age" : 21,
"contactno" : 6398193339,
"e-mail" : "sudha@gmail.com",
"creditscore" : "A"
}
>
Now change creditscore to person named Vamsi to “B”
> db.student.update({name:"Vamsi"},{$set:{creditscore:"B"}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find().pretty()
{
"_id" : ObjectId("5d671076ae0af543597ceb16"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848912299,
"e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A"
}
{
"_id" : ObjectId("5d6710cfae0af543597ceb17"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848191299,
"e-mail" : "krishna@gmail.com",
"creditscore" : "A"
}
{
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #22
"_id" : ObjectId("5d67112bae0af543597ceb18"),
"rollno" : 17303,
"name" : "Vamsi",
"age" : 22,
"contactno" : 7988811299,
"e-mail" : "vamsi@gmail.com",
"creditscore" : "B"
}
{
"_id" : ObjectId("5d671175ae0af543597ceb19"),
"rollno" : 17304,
"name" : "Sudha",
"age" : 21,
"contactno" : 6398193339,
"e-mail" : "sudha@gmail.com",
"creditscore" : "A"
}
>
Here credit score for Vamsi has been changed to – B
1. collection.updateOne() This update statement updates the FIRST document that matches
the given filter. Filter is any criteria based on which we want to
update document
db.student.updateOne( This update commands use the
{ "age": 18 }, age =18 as a filter (match the query) in the collection “students”.
{
$set: { "canVote": "True" }, $set operator (called as update operators) updates the value of the
canVote to True.
} To update multiple parameters, parameters must be separated by a
) comma (,).
e.g.: $set: { "canVote": "True", “isAdult”=”Yes” },
3. collection.findOneAndUpdate() This updates a single document based on the filter and other
criteria. The simple syntax is:
db.collection.findOneAndUpdate(filter, update, options)
db.collection.findOneAndUpdate( Here, we have some additional parameters:
<filter>,
<update>,
{ Projection: Optional. It denotes A subset of fields to return. If you
projection: <document>, want to return all fields in the returned document, do not include this
parameter.
sort: <document>, Sort: Specifies a sorting order for the documents matched by the
filter.
returnNewDocument: <boolean> returnNewDocument: It returns the updated document instead of
} the original document when set to true.
)
4. collection.replaceOne() This command replaces at most a single document (replace an
existing document) that matches a specified filter even though
multiple documents may match the specified filter.
db.collection.replaceOne(
<filter>,
<replacement>,
{ Here, upsert is of boolean type. upsert can be either true or false.
upsert: <boolean>, If set to true, it creates a new document when no document
matches the query criteria.
writeConcern: <document> The default value is false, which does not insert a new document
} when no match is found.
)
5.collection.findOneAndReplace() This command modifies and replaces a single document based on
the filter and sort criteria.
db.collection.findOneAndReplace(
<filter>,
<replacement>,
{
projection: <document>,
sort: <document>,
maxTimeMS: <number>,
upsert: <boolean>,
returnNewDocument: <boolean>
}
)
6. collection.findAndModify() This command modifies and returns a single document. But the
key point to be noted here is that by default, the returned document
does not include the modifications made on the update. To return
the document with the modifications made on the update, use the
new option.
db.collection.findAndModify({
query: <document>,
sort: <document>,
remove: <boolean>,
update: <document>,
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #24
new: <boolean>,
fields: <document>,
upsert: <boolean>,
bypassDocumentValidation:
<boolean>,
writeConcern: <document>
});
> db.student.find().pretty()
{
"_id" : ObjectId("5d671076ae0af543597ceb16"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848912299,
"e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d6710cfae0af543597ceb17"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848191299,
"e-mail" : "krishna@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d67112bae0af543597ceb18"),
"rollno" : 17303,
"name" : "Vamsi",
"age" : 22,
"contactno" : 7988811299,
"e-mail" : "vamsi@gmail.com",
"creditscore" : "B",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d671175ae0af543597ceb19"),
"rollno" : 17304,
"name" : "Sudha",
"age" : 21,
"contactno" : 6398193339,
"e-mail" : "sudha@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d6729f4ae0af543597ceb1a"),
"rollno" : 17305,
"name" : "Snigdha",
"age" : 21,
"contactno" : 8383118383,
"e-mail" : "snigdha@gmail.com",
"creditscore" : "A",
"location" : "Amaravati",
"hobbies" : "acting"
}
{
"_id" : ObjectId("5d672ac8ae0af543597ceb1b"),
"rollno" : 17306,
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #26
"name" : "Anamika",
"age" : 21,
"contactno" : 9833194383,
"e-mail" : "anamika@gmail.com",
"creditscore" : "A",
"location" : "Anantapuram",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d672b1fae0af543597ceb1c"),
"rollno" : 17306,
"name" : "Pravallika",
"age" : 20,
"contactno" : 8982212383,
"e-mail" : "pravallika@gmail.com",
"creditscore" : "B",
"location" : "Nellore",
"hobbies" : "chess"
}
>
1. collection.remove()
2. collection.deleteOne() – New in version 3.2.
3. collection.deleteMany() – New in version 3.2.
4. collection.findOneAndDelete()
This method deletes a single document or all documents from the collection that match a specified filter.
db.collection.remove() method (remove method) works on two parameters.
Deletion criteria: Condition on which document has to be deleted. (Match a deletion criteria)
JustOne: To remove only one document when set to true or 1.
>db.student.remove({age : 18 } )
This command will delete all the documents from the ‘student’ collection where ‘age’ field is equal to ‘18’
If you want to remove a single document that matches a given condition, then use the following syntax.
>db.student.remove({age : 18 }, 1 )
Note:To delete all the documents from the collection (i.e. pass an empty filter without any condition)
>db.student.remove({})
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #27
2. db.collection.deleteOne()
This method deletes at most a single document that matches a specified filter even though multiple
documents may match the specified filter. (MongoDB in v3.2)
3. db.collection.deleteMany()
This method deletes all documents that match a specified filter. (MongoDB v3.2)
db.student.deleteMany({age : 17})
This command will delete all the documents from the ‘student’ collection where ‘age’ field is equal to ‘17’.
4) db.collection.findOneAndDelete()
This method deletes a single document based on the filter and sort criteria, returning the deleted document.
db.collection.findOneAndDelete(
<filter>,
{
projection: <document>,
sort: <document>,
maxTimeMS: <number>,
}
)
Objective: To remove the field "Location" with value "Nellore" in the document of "Students"
collection.
>db.student.update({“name”:”Pravallika”},{$unset:{Location:”Nellore”});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
Outcome: Confirm if the stated field ("Location") has been dropped from the document of the "Students"
collection.
Task1: Find the document wherein the “Name” has value "Anamika" in students collection
>db.student.find({“name”: "Anamika"}).pretty(); output:{
"_id" : ObjectId("5d672ac8ae0af543597ceb1b"),
"rollno" : 17307,
"name" : "Anamika",
"age" : 21,
"contactno" : 9833194383,
"e-mail" : "anamika@gmail.com",
"creditscore" : "A",
"location" : "Anantapuram",
"hobbies" : "sports"
}
>
Task2: Find the document wherein the “creditscore” has value "A" in students collection
>db.student.find({“creditscore”: "A"}).pretty(); Output:
{
"_id" : ObjectId("5d671076ae0af543597ceb16"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848912299,
"e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d6710cfae0af543597ceb17"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848191299,
"e-mail" : "krishna@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
...
{
"_id" : ObjectId("5d672ac8ae0af543597ceb1b"),
"rollno" : 17306,
"name" : "Anamika",
"age" : 21,
"contactno" : 9833194383,
"e-mail" : "anamika@gmail.com",
"creditscore" : "A",
"location" : "Anantapuram",
"hobbies" : "sports"
}
>
Task3: Find the document wherein the “creditscore” has value not equal to "A" in students collection
>db.student.find({“creditscore”: {$ne:"A"}}).pretty(); Output:
{
"_id" : ObjectId("5d67112bae0af543597ceb18"),
"rollno" : 17303,
"name" : "Vamsi",
"age" : 22,
"contactno" : 7988811299,
"e-mail" : "vamsi@gmail.com",
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #29
"creditscore" : "B",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d672b1fae0af543597ceb1c"),
"rollno" : 17306,
"name" : "Pravallika",
"age" : 20,
"contactno" : 8982212383,
"e-mail" : "pravallika@gmail.com",
"creditscore" : "B",
"location" : "Nellore",
"hobbies" : "chess"
}
>
Task5: Find the document wherein the “hobbies” are neither "chess" nor "acting" in students
collection
>db.student.find({“hobbies”: {$nin: Output:
["chess","acting"]}}).pretty(); {
"_id" : ObjectId("5d671076ae0af543597ceb16"),
"rollno" : 17301,
"name" : "Murali",
Here - and operation by "age" : 23,
{ key: {$nin:[value,value]}} "contactno" : 9848912299,
"e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #30
"_id" : ObjectId("5d6710cfae0af543597ceb17"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848191299,
"e-mail" : "krishna@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
...
{
"_id" : ObjectId("5d672ac8ae0af543597ceb1b"),
"rollno" : 17307,
"name" : "Anamika",
"age" : 21,
"contactno" : 9833194383,
"e-mail" : "anamika@gmail.com",
"creditscore" : "A",
"location" : "Anantapuram",
"hobbies" : "sports"
}
>
AND condition
Task6: Find the document wherein the “hobbies” is sports and "location" Vijayawada in students collection
Output:
>db.student.find({"hobbies":"sports","location":"Vijayawa {
da"}).pretty(); "_id" : ObjectId("5d671076ae0af543597ceb16"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
Here - and operation by separating simply by comma "contactno" : 9848912299,
key:value,key:value "e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
hobbies:"sports",location:"Vijayawada" "hobbies" : "sports"
}
{
"_id" : ObjectId("5d6710cfae0af543597ceb17"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848191299,
"e-mail" : "krishna@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
...
>
Task7: To find documents from the Students collection where the name begins with "S"
Output:
>db.student.find({"name":/^S/}).pretty(); {
"_id" : ObjectId("5d671175ae0af543597ceb19"),
"rollno" : 17304,
"name" : "Sudha",
/^S/ - indicate string beginning with S in regular "age" : 21,
expression "contactno" : 6398193339,
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #31
"e-mail" : "sudha@gmail.com",
Note: "creditscore" : "A",
"location" : "Vijayawada",
Here “//” specifies that our search criteria is within "hobbies" : "sports"
these delimiters }
{
"_id" : ObjectId("5d6729f4ae0af543597ceb1a"),
"rollno" : 17305,
"name" : "Snigdha",
"age" : 21,
"contactno" : 8383118383,
"e-mail" : "snigdha@gmail.com",
"creditscore" : "A",
"location" : "Amaravati",
"hobbies" : "acting"
}
>
Task8: To find documents from the Students collection where the name end with "i"
Output:
>db.student.find({"name":/i$/}).pretty(); {
"_id" : ObjectId("5d671076ae0af543597ceb16"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848912299,
/i$/ - indicate string ending with i in regular expression "e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d67112bae0af543597ceb18"),
"rollno" : 17303,
"name" : "Vamsi",
"age" : 22,
"contactno" : 7988811299,
"e-mail" : "vamsi@gmail.com",
"creditscore" : "B",
"location" : "Vijayawada",
"hobbies" : "sports"
}
>
Task9: To find documents from the Students collection where the name has an "v" in any position
db.student.find({name:/v/}) .pretty (); Output:
OR {
db.student.find({name:/.*v.*/}) .prettyO; "_id" : ObjectId("5d672b1fae0af543597ceb1c"),
"rollno" : 17306,
OR "name" : "Pravallika",
db.student.find({name:{$regex:"v"}}).pretty(); "age" : 20,
"contactno" : 8982212383,
"e-mail" : "pravallika@gmail.com",
"creditscore" : "B",
"location" : "Nellore",
"hobbies" : "chess"
}
>
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #32
Using regex
Task10: To find documents from the Students collection where the name begins with "S" - Using regex
Output:
>db.student.find({name:{$regex:"^S"}}).pretty(); {
"_id" : ObjectId("5d671175ae0af543597ceb19"),
"rollno" : 17304,
"name" : "Sudha",
"age" : 21,
"contactno" : 6398193339,
"e-mail" : "sudha@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d6729f4ae0af543597ceb1a"),
"rollno" : 17305,
"name" : "Snigdha",
"age" : 21,
"contactno" : 8383118383,
"e-mail" : "snigdha@gmail.com",
"creditscore" : "A",
"location" : "Amaravati",
"hobbies" : "acting"
}
>
Task11: To find documents from the Students collection where the name end with "i"- using regex
Output:
>db.student.find({"name":{$regex:"i$"}}).pretty(); {
"_id" : ObjectId("5d671076ae0af543597ceb16"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848912299,
"e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d67112bae0af543597ceb18"),
"rollno" : 17303,
"name" : "Vamsi",
"age" : 22,
"contactno" : 7988811299,
"e-mail" : "vamsi@gmail.com",
"creditscore" : "B",
"location" : "Vijayawada",
"hobbies" : "sports"
}
>
>db.student.count();
7
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #33
Task2: Count the number of the documents in the student collection where location is Viajaywada.
>db.student.count({location:”Vijayawada”});
4
Task3: retrieve the first 2 documents in the student collection where location is Viajaywada.
2.limit
Syntax: >db.COLLECTION_name.find().limit(NUMBER)
>db.student.find({location:”Vijayawada”}).limit(2).pretty()
output:
{
"_id" : ObjectId("5d671076ae0af543597ceb16"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848912299,
"e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d6710cfae0af543597ceb17"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848191299,
"e-mail" : "krishna@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
3. Skip
skip() which also accepts number type argument and is used to skip the number of documents.
Syntax: db.COLLECTION_name.find().limit(NUMBER).skip(NUMBER)
Note: The default value in skip() method is 0 (zero)
>db.student.find().skip(4).pretty()
out put:
{
"_id" : ObjectId("5d6729f4ae0af543597ceb1a"),
"rollno" : 17305,
"name" : "Snigdha",
"age" : 21,
"contactno" : 8383118383,
"e-mail" : "snigdha@gmail.com",
"creditscore" : "A",
"location" : "Amaravati",
"hobbies" : "acting"
}
{
"_id" : ObjectId("5d672ac8ae0af543597ceb1b"),
"rollno" : 17307,
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #34
"name" : "Anamika",
"age" : 21,
"contactno" : 9833194383,
"e-mail" : "anamika@gmail.com",
"creditscore" : "A",
"location" : "Anantapuram",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d672b1fae0af543597ceb1c"),
"rollno" : 17306,
"name" : "Pravallika",
"age" : 20,
"contactno" : 8982212383,
"e-mail" : "pravallika@gmail.com",
"creditscore" : "B",
"location" : "Nellore",
"hobbies" : "chess"
}
>
Task2: Skip first 4 documents from students collection limit to 2 documents
>db.student.find().skip(4).limit(2).pretty()
out put:
{
"_id" : ObjectId("5d6729f4ae0af543597ceb1a"),
"rollno" : 17305,
"name" : "Snigdha",
"age" : 21,
"contactno" : 8383118383,
"e-mail" : "snigdha@gmail.com",
"creditscore" : "A",
"location" : "Amaravati",
"hobbies" : "acting"
}
{
"_id" : ObjectId("5d672ac8ae0af543597ceb1b"),
"rollno" : 17307,
"name" : "Anamika",
"age" : 21,
"contactno" : 9833194383,
"e-mail" : "anamika@gmail.com",
"creditscore" : "A",
"location" : "Anantapuram",
"hobbies" : "sports"
}
>
Here we got documents from 5 th onwards by skip(4) and listing only 2 documents as limit(2)
>db.student.find().pretty().skip(db.student.count()-2)
{
"_id" : ObjectId("5d69db5b027d622d21d17ada"),
"rollno" : 17306,
"age" : 20,
"contactno" : 8982212383,
"e-mail" : "pravallika@gmail.com",
"creditscore" : "B",
"location" : "Nellore",
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #35
"hobbies" : "chess",
"name" : "Pravallika"
}
{
"_id" : ObjectId("5d69dbe1027d622d21d17adb"),
"rollno" : 17307,
"age" : 21,
"contactno" : 9833194383,
"e-mail" : "anamika@gmail.com",
"creditscore" : "A",
"location" : "Anantapuram",
"hobbies" : "sports",
"name" : "Anamika"
}
>
Task 4: To retrieve the third, fourth, and fifth document from the student collection.
db.student.find().pretty().skip(2).limit(3)
Here we are skipping first two documents with skip(2), then displaying next three documents by limit(3)
>db.student.find().pretty().skip(2).limit(3)
{
"_id" : ObjectId("5d69d6e18cd7279cccb0f494"),
"age" : 23,
"name" : "Vamsi",
"rollno" : 17303,
"contactno" : 7988811299,
"e-mail" : "vamsi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d69d792027d622d21d17ad8"),
"rollno" : 17304,
"name" : "Sudha",
"age" : 21,
"contactno" : 6398193339,
"e-mail" : "sudha@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d69dad7027d622d21d17ad9"),
"rollno" : 17305,
"age" : 21,
"contactno" : 8383118383,
"e-mail" : "snigdha@gmail.com",
"creditscore" : "A",
"location" : "Amaravati",
"hobbies" : "acting",
"name" : "Snigdha"
}
>
4. Sort
Task 1: To sort the documents from the Student collection name in ascending order
db.student.find().sort({name:1}).pretty();
{
"_id" : ObjectId("5d69dbe1027d622d21d17adb"),
"rollno" : 17307,
"name" : "Anamika",
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #36
"age" : 21,
"contactno" : 9833194383,
"e-mail" : "anamika@gmail.com",
"creditscore" : "A",
"location" : "Anantapuram",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d69d6bf027d622d21d17ad7"),
"rollno" : 17302,
"name" : "Krishna",
"age" : 23,
"contactno" : 9848111299,
"e-mail" : "krishna@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d69d63d4dfd101ef5eb9bb1"),
"rollno" : 17301,
"name" : "Murali",
"age" : 23,
"contactno" : 9848929299,
"e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d69db5b027d622d21d17ada"),
"rollno" : 17306,
"name" : "Pravallika",
"age" : 20,
"contactno" : 8982212383,
"e-mail" : "pravallika@gmail.com",
"creditscore" : "B",
"location" : "Nellore",
"hobbies" : "chess"
}
{
"_id" : ObjectId("5d69dad7027d622d21d17ad9"),
"rollno" : 17305,
"name" : "Snigdha",
"age" : 21,
"contactno" : 8383118383,
"e-mail" : "snigdha@gmail.com",
"creditscore" : "A",
"location" : "Amaravati",
"hobbies" : "acting"
}
Task2: To sort the documents from the student collection first on name in ascending order and then on
Hobbies in desending order.
db.student.find().sort({name: 1, hobbies: -1}).pretty()
Here documents are sorted first on name in ascending order(1) then hobbies in descending order (-1)
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #37
"location" : "Vijayawada",
"hobbies" : "sports"
}
{
"_id" : ObjectId("5d69d6e18cd7279cccb0f494"),
"age" : 23,
"name" : "Vamsi",
"rollno" : 17303,
"contactno" : 7988811299,
"e-mail" : "vamsi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
>
Task3: To sort the documents from student collection by name and skip the first two documents from sorted list
Syntax : db.student.find().skip(2).pretty().sort({name:1})
> db.student.find().skip(2).pretty().sort({name:1})
{
"_id" : ObjectId("5d69d63d4dfd101ef5eb9bb1"),
"rollno" : 17301,
"age" : 23,
"contactno" : 9848929299,
"e-mail" : "muralivadlamudi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports",
"name" : "Murali"
}
{
"_id" : ObSyntaxjectId("5d69db5b027d622d21d17ada"),
"rollno" : 17306,
"age" : 20,
"contactno" : 8982212383,
"e-mail" : "pravallika@gmail.com",
"creditscore" : "B",
"location" : "Nellore",
"hobbies" : "chess",
"name" : "Pravallika"
}
{
"_id" : ObjectId("5d69dad7027d622d21d17ad9"),
"rollno" : 17305,
"age" : 21,
"contactno" : 8383118383,
"e-mail" : "snigdha@gmail.com",
"creditscore" : "A",
"location" : "Amaravati",
"hobbies" : "acting",
"name" : "Snigdha"
}
{
"_id" : ObjectId("5d69d792027d622d21d17ad8"),
"rollno" : 17304,
"name" : "Sudha",
"age" : 21,
"contactno" : 6398193339,
"e-mail" : "sudha@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #39
{
"_id" : ObjectId("5d69d6e18cd7279cccb0f494"),
"age" : 23,
"name" : "Vamsi",
"rollno" : 17303,
"contactno" : 7988811299,
"e-mail" : "vamsi@gmail.com",
"creditscore" : "A",
"location" : "Vijayawada",
"hobbies" : "sports"
}
>
NULL values
To add new field with null value in existing documents of students (either to specified criteria or to all)
A NULL is a missing or unknown value. When null value is placed for a field it indicate that we did not know
value or value missing.
But we can always update the value NULL later.
Existing documents in student collection
> db.student.find()
{ "_id" : ObjectId("5d69d63d4dfd101ef5eb9bb1"), "rollno" : 17301, "age" : 23, "contactno" : 9848929299, "e-
mail" : "muralivadlamudi@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" : "sports",
"name" : "Murali" }
{ "_id" : ObjectId("5d69d6bf027d622d21d17ad7"), "rollno" : 17302, "age" : 23, "contactno" : 9848111299, "e-
mail" : "krishna@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" : "sports", "name" :
"Krishna" }
{ "_id" : ObjectId("5d69d6e18cd7279cccb0f494"), "age" : 23, "name" : "Vamsi", "rollno" : 17303,
"contactno" : 7988811299, "e-mail" : "vamsi@gmail.com", "creditscore" : "A", "location" : "Vijayawada",
"hobbies" : "sports" }
{ "_id" : ObjectId("5d69d792027d622d21d17ad8"), "rollno" : 17304, "name" : "Sudha", "age" : 21, "contactno"
: 6398193339, "e-mail" : "sudha@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" :
"sports" }
{ "_id" : ObjectId("5d69dad7027d622d21d17ad9"), "rollno" : 17305, "age" : 21, "contactno" : 8383118383, "e-
mail" : "snigdha@gmail.com", "creditscore" : "A", "location" : "Amaravati", "hobbies" : "acting", "name" :
"Snigdha" }
{ "_id" : ObjectId("5d69db5b027d622d21d17ada"), "rollno" : 17306, "age" : 20, "contactno" : 8982212383, "e-
mail" : "pravallika@gmail.com", "creditscore" : "B", "location" : "Nellore", "hobbies" : "chess", "name" :
"Pravallika" }
{ "_id" : ObjectId("5d69dbe1027d622d21d17adb"), "rollno" : 17307, "age" : 21, "contactno" : 9833194383, "e-
mail" : "anamika@gmail.com", "creditscore" : "A", "location" : "Anantapuram", "hobbies" : "sports", "name" :
"Anamika" }
>
Task1: We add new field “disability” and assign null to all documents
>db.student.update({},{$set:{disability:null}})
> db.student.find()
{ "_id" : ObjectId("5d69d63d4dfd101ef5eb9bb1"), "rollno" : 17301, "age" : 23, "contactno" : 9848929299, "e-
mail" : "muralivadlamudi@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" : "sports",
"name" : "Murali", "disability" : null } ←Here one document is updated as we specified update
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #40
←Here one document is updated as we specified update, if we need to update all documents we need to specify
updateMany()
db.student.updateMany({},{$set:{disability:null}})
{ "acknowledged" : true, "matchedCount" : 7, "modifiedCount" : 6 }
> db.student.find()
{ "_id" : ObjectId("5d69d63d4dfd101ef5eb9bb1"), "rollno" : 17301, "age" : 23, "contactno" : 9848929299, "e-
mail" : "muralivadlamudi@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" : "sports",
"name" : "Murali", "disability" : null }
{ "_id" : ObjectId("5d69d6bf027d622d21d17ad7"), "rollno" : 17302, "age" : 23, "contactno" : 9848111299, "e-
mail" : "krishna@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" : "sports", "name" :
"Krishna", "disability" : null }
{ "_id" : ObjectId("5d69d6e18cd7279cccb0f494"), "age" : 23, "name" : "Vamsi", "rollno" : 17303,
"contactno" : 7988811299, "e-mail" : "vamsi@gmail.com", "creditscore" : "A", "location" : "Vijayawada",
"hobbies" : "sports", "disability" : null }
{ "_id" : ObjectId("5d69d792027d622d21d17ad8"), "rollno" : 17304, "name" : "Sudha", "age" : 21, "contactno"
: 6398193339, "e-mail" : "sudha@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" :
"sports", "disability" : null }
{ "_id" : ObjectId("5d69dad7027d622d21d17ad9"), "rollno" : 17305, "age" : 21, "contactno" : 8383118383, "e-
mail" : "snigdha@gmail.com", "creditscore" : "A", "location" : "Amaravati", "hobbies" : "acting", "name" :
"Snigdha", "disability" : null }
{ "_id" : ObjectId("5d69db5b027d622d21d17ada"), "rollno" : 17306, "age" : 20, "contactno" : 8982212383, "e-
mail" : "pravallika@gmail.com", "creditscore" : "B", "location" : "Nellore", "hobbies" : "chess", "name" :
"Pravallika", "disability" : null }
{ "_id" : ObjectId("5d69dbe1027d622d21d17adb"), "rollno" : 17307, "age" : 21, "contactno" : 9833194383, "e-
mail" : "anamika@gmail.com", "creditscore" : "A", "location" : "Anantapuram", "hobbies" : "sports", "name" :
"Anamika", "disability" : null }
>
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #41
Task3 : assign a value:”Physically challenged” to field disability for a document whose name is Vamsi
db.student.update({name:”Vamsi”},{$set:{disability:”Physically Challenged”}})
>db.student.update({name:"Vamsi"},{$set:{disability:"Physically Challenged"}})
> db.student.find()
{ "_id" : ObjectId("5d69d63d4dfd101ef5eb9bb1"), "rollno" : 17301, "age" : 23, "contactno" : 9848929299, "e-
mail" : "muralivadlamudi@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" : "sports",
"name" : "Murali", "disability" : null }
{ "_id" : ObjectId("5d69d6bf027d622d21d17ad7"), "rollno" : 17302, "age" : 23, "contactno" : 9848111299, "e-
mail" : "krishna@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" : "sports", "name" :
"Krishna", "disability" : null }
{ "_id" : ObjectId("5d69d6e18cd7279cccb0f494"), "age" : 23, "name" : "Vamsi", "rollno" : 17303,
"contactno" : 7988811299, "e-mail" : "vamsi@gmail.com", "creditscore" : "A", "location" : "Vijayawada",
"hobbies" : "sports", "disability" : "Physically Challenged" } ←Here one document is updated
{ "_id" : ObjectId("5d69d792027d622d21d17ad8"), "rollno" : 17304, "name" : "Sudha", "age" : 21, "contactno"
: 6398193339, "e-mail" : "sudha@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" :
"sports", "disability" : null }
{ "_id" : ObjectId("5d69dad7027d622d21d17ad9"), "rollno" : 17305, "age" : 21, "contactno" : 8383118383, "e-
mail" : "snigdha@gmail.com", "creditscore" : "A", "location" : "Amaravati", "hobbies" : "acting", "name" :
"Snigdha", "disability" : null }
{ "_id" : ObjectId("5d69db5b027d622d21d17ada"), "rollno" : 17306, "age" : 20, "contactno" : 8982212383, "e-
mail" : "pravallika@gmail.com", "creditscore" : "B", "location" : "Nellore", "hobbies" : "chess", "name" :
"Pravallika", "disability" : null }
{ "_id" : ObjectId("5d69dbe1027d622d21d17adb"), "rollno" : 17307, "age" : 21, "contactno" : 9833194383, "e-
mail" : "anamika@gmail.com", "creditscore" : "A", "location" : "Anantapuram", "hobbies" : "sports", "name" :
"Anamika", "disability" : null }
>
Task4: Remove the disability field from student collection where value is assigned “Physically
Challenged”
>db.student.update({name:"Vamsi"},{$unset:{disability:"Physically Challenged"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.student.find()
{ "_id" : ObjectId("5d69d63d4dfd101ef5eb9bb1"), "rollno" : 17301, "age" : 23, "contactno" : 9848929299, "e-
mail" : "muralivadlamudi@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" : "sports",
"name" : "Murali", "disability" : null }
{ "_id" : ObjectId("5d69d6bf027d622d21d17ad7"), "rollno" : 17302, "age" : 23, "contactno" : 9848111299, "e-
mail" : "krishna@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" : "sports", "name" :
"Krishna", "disability" : null }
{ "_id" : ObjectId("5d69d6e18cd7279cccb0f494"), "age" : 23, "name" : "Vamsi", "rollno" : 17303,
"contactno" : 7988811299, "e-mail" : "vamsi@gmail.com", "creditscore" : "A", "location" : "Vijayawada",
"hobbies" : "sports" } ←Here one document is updated and field disability is deleted
{ "_id" : ObjectId("5d69d792027d622d21d17ad8"), "rollno" : 17304, "name" : "Sudha", "age" : 21, "contactno"
: 6398193339, "e-mail" : "sudha@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" :
"sports", "disability" : null }
{ "_id" : ObjectId("5d69dad7027d622d21d17ad9"), "rollno" : 17305, "age" : 21, "contactno" : 8383118383, "e-
mail" : "snigdha@gmail.com", "creditscore" : "A", "location" : "Amaravati", "hobbies" : "acting", "name" :
"Snigdha", "disability" : null }
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #42
> db.student.find()
{ "_id" : ObjectId("5d69d63d4dfd101ef5eb9bb1"), "rollno" : 17301, "age" : 23, "contactno" : 9848929299, "e-
mail" : "muralivadlamudi@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" : "sports",
"name" : "Murali" }
{ "_id" : ObjectId("5d69d6bf027d622d21d17ad7"), "rollno" : 17302, "age" : 23, "contactno" : 9848111299, "e-
mail" : "krishna@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" : "sports", "name" :
"Krishna" }
{ "_id" : ObjectId("5d69d6e18cd7279cccb0f494"), "age" : 23, "name" : "Vamsi", "rollno" : 17303,
"contactno" : 7988811299, "e-mail" : "vamsi@gmail.com", "creditscore" : "A", "location" : "Vijayawada",
"hobbies" : "sports" }
{ "_id" : ObjectId("5d69d792027d622d21d17ad8"), "rollno" : 17304, "name" : "Sudha", "age" : 21, "contactno"
: 6398193339, "e-mail" : "sudha@gmail.com", "creditscore" : "A", "location" : "Vijayawada", "hobbies" :
"sports" }
{ "_id" : ObjectId("5d69dad7027d622d21d17ad9"), "rollno" : 17305, "age" : 21, "contactno" : 8383118383, "e-
mail" : "snigdha@gmail.com", "creditscore" : "A", "location" : "Amaravati", "hobbies" : "acting", "name" :
"Snigdha" }
{ "_id" : ObjectId("5d69db5b027d622d21d17ada"), "rollno" : 17306, "age" : 20, "contactno" : 8982212383, "e-
mail" : "pravallika@gmail.com", "creditscore" : "B", "location" : "Nellore", "hobbies" : "chess", "name" :
"Pravallika" }
{ "_id" : ObjectId("5d69dbe1027d622d21d17adb"), "rollno" : 17307, "age" : 21, "contactno" : 9833194383, "e-
mail" : "anamika@gmail.com", "creditscore" : "A", "location" : "Anantapuram", "hobbies" : "sports", "name" :
"Anamika" }
>
Creation of bulk documents in to collection at one stretch
Step1: Create a variable at mongo prompt
>var variable =
[
{ key:value,
key:value,
key:value
},
{ key:value,
key:value,
key:value
},
{ key:value,
key:value,
key:value
},
];
after execution you will get > prompt
>
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #43
... "age":32,
... "qual":"MCA",
... "role":"Lecturer",
... "dept":"Computers",
... "exp":8,
... "basic":12000
... },
... ];
>
Step2: Assign values in to a collection(employee)in current database
> db.employee.insert(emp);
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 6,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
>
Step3: display documents in collection
> db.employee.find().pretty()
{
"_id" : ObjectId("5d7166896167c454bdec28fd"),
"eno" : 1001,
"ename" : "Murali",
"age" : 30,
"qual" : "M.Sc(Maths)",
"role" : "Lecturer",
"dept" : "Mathematics",
"exp" : 7,
"basic" : 12000
}
{
"_id" : ObjectId("5d7166896167c454bdec28fe"),
"eno" : 1002,
"ename" : "Krishna",
"age" : 33,
"qual" : "M.Sc(Maths)",
"role" : "Lecturer",
"dept" : "Mathematics",
"exp" : 5,
"basic" : 10000
}
{
"_id" : ObjectId("5d7166896167c454bdec28ff"),
"eno" : 1003,
"ename" : "Vamsi",
"age" : 33,
"qual" : "M.Sc(Physics)",
"role" : "Lecturer",
"dept" : "Physics",
"exp" : 6,
"basic" : 11000
}
{
"_id" : ObjectId("5d7166896167c454bdec2900"),
"eno" : 1004,
"ename" : "Sai Krishna",
"age" : 33,
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #45
"qual" : "M.Sc(Computers)",
"role" : "Lecturer",
"dept" : "Computers",
"exp" : 8,
"basic" : 12000
}
{
"_id" : ObjectId("5d7166896167c454bdec2901"),
"eno" : 1005,
"ename" : "Snigdha",
"age" : 33,
"qual" : "MCA",
"role" : "Lecturer",
"dept" : "Computers",
"exp" : 8,
"basic" : 12000
}
{
"_id" : ObjectId("5d7166896167c454bdec2902"),
"eno" : 1006,
"ename" : "Anamika",
"age" : 32,
"qual" : "MCA",
"role" : "Lecturer",
"dept" : "Computers",
"exp" : 8,
"basic" : 12000
}
>
6.5.8 Arrays
These MongoDB data types stores array. A set of values are represented as an array. This data type can store
multiples of values and data types.
Task: Find out the document fruit array contain "banana", "apple", "cherry" in food collection
>db.food.find({fruits:
["banana","apple","cherry"]}).pretty()
{
"_id" : ObjectId("5d716e876167c454bdec2903"),
"fruits" : [
"banana",
"apple",
"cherry"
]
}
To list documents from the "food" collection which has the "fruits" array having "banana", as an
element.
> db.food.find({fruits:"banana"})
{ "_id" : ObjectId("5d716e876167c454bdec2903"), "fruits" : [ "banana",
"apple", "cherry" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2906"), "fruits" : [ "banana",
"strawbeery", "grapes" ] }
>
To find documents from the "food" collection where "grapes" is present in the 1st index position of the
"fruits" array
> db.food.find({"fruits.1":"grapes"}).pretty()
{
"_id" : ObjectId("5d716e876167c454bdec2907"),
"fruits" : [
"orange",
"grapes"
]
}
>
To find documents from the "food" collection where "grapes" is present in the 2nd index position of the
"fruits" array
> db.food.find({"fruits.2":"grapes"})
{ "_id" : ObjectId("5d716e876167c454bdec2905"), "fruits" : [ "pineapple",
"strawberry", "grapes" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2906"), "fruits" : [ "banana", "strawbeery",
"grapes" ] }
>
To find and list documents from the "food" collection where the size of the array is two. The size
implies that the array holds only 2 values.
db.food.find({fruits:{$size:2}}) Here $size:2 identify
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "grapes" ] } documents with array
> size as 2
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #47
To find those documents from the "food" collection where the size of the array is three. Array with 3
values.
> db.food.find({fruits:{$size:3}})
{ "_id" : ObjectId("5d716e876167c454bdec2903"), "fruits" : [ "banana", "apple", "cherry" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2904"), "fruits" : [ "orange", "butterfruit", "mango" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2905"), "fruits" : [ "pineapple", "strawberry", "grapes" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2906"), "fruits" : [ "banana", "strawbeery", "grapes" ] }
>
To find the documents from array, whose size is 3 from the "food" collection and display the first two
elements
> db.food.find({fruits:{$size:3}},{fruits:{$slice:2}})
{ "_id" : ObjectId("5d716e876167c454bdec2903"), "fruits" : [ "banana", "apple" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2904"), "fruits" : [ "orange", "butterfruit" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2905"), "fruits" : [ "pineapple", "strawberry" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2906"), "fruits" : [ "banana", "strawbeery" ] }
>
To find all documents from the "food" collection which have elements "orange" and "grapes" in the array
"fruits".
> db.food.find({fruits:["orange","grapes"]})
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "grapes" ] }
>
To find those documents from the "food" collection which have the element "orange" in the 0 th index position in
the array "fruits".
> db.food.find({"fruits.0":"orange"})
{ "_id" : ObjectId("5d716e876167c454bdec2904"), "fruits" : [ "orange", "butterfruit", "mango" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "grapes" ] }
>
To find those documents from the "food" collection which have the element "orange" in the 0 th index position in
the array "fruits". Display two elements, starting with the element at 1st index position.
> db.food.find({"fruits.0":"orange"},{fruits:{$slice:[1,2]}})
{ "_id" : ObjectId("5d716e876167c454bdec2904"), "fruits" : [ "butterfruit", "mango" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "grapes" ] }
>
After action:
> db.food.find({"fruits.1":"jackfruit"} )
{ "_id" : ObjectId("5d716e876167c454bdec2904"), "fruits" : [ "orange", "jackfruit", "mango" ] }
>
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #48
To update the document where “grapes” is at 1st index position and replace new value in the "fruits"
array with “apple”
before
> db.food.find({"fruits.1":"grapes"})
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "grapes" ] }
after:
> db.food.update({"fruits.1":"grapes"},{$set:{"fruits.1":"apple"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
result:
> db.food.find({"fruits.1":"apple"})
{ "_id" : ObjectId("5d716e876167c454bdec2903"), "fruits" : [ "banana", "apple", "cherry" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "apple" ] }
>
To update the documents where “apple” is at 1st index and replace new value pairs in the "fruits" array
with “an apple”
before:
> db.food.find({"fruits.1":"apple"})
{ "_id" : ObjectId("5d716e876167c454bdec2903"), "fruits" : [ "banana", "apple", "cherry" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "apple" ] }
Process:
> db.food.update({"fruits.1":"apple"},{$set:{"fruits.1":"an apple"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Result
> db.food.find({"fruits.1":"an apple"})
{ "_id" : ObjectId("5d716e876167c454bdec2903"), "fruits" : [ "banana", "an apple", "cherry" ] }
>
Here “apple” is replaced “an apple” in only first occurrence of document in collection as we used db.food.update.
If we use db.food.updateMany, then “apple” is replaced with “an apple” in its all occurrences
To update the document with "apple" at index 1 and push new value “cherry” in to the "fruits" array.
Before:
> db.food.find({"fruits.1":"apple"})
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "apple" ] }
Process:
> db.food.update({"fruits.1":"apple"},{$push:{fruits:"cherry"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
after:
> db.food.find({"fruits.1":"apple"})
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "apple", "cherry" ] }
>
To update the document with "orange at 0th index and push new key value pairs in the "fruits" array.
Add new array price
Before:
> db.food.find()
{ "_id" : ObjectId("5d716e876167c454bdec2903"), "fruits" : [ "banana", "an apple", "cherry" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2904"), "fruits" : [ "orange", "jackfruit", "mango" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2905"), "fruits" : [ "pineapple", "strawberry", "grapes" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2906"), "fruits" : [ "banana", "strawbeery", "grapes" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "apple" ] }
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #49
Process:
> db.food.update({"fruits.0":"orange"},{$push:{price:{orange:60,jackfruit:200,mango:120}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
After:
> db.food.find()
{ "_id" : ObjectId("5d716e876167c454bdec2903"), "fruits" : [ "banana", "an apple", "cherry" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2904"), "fruits" : [ "orange", "jackfruit", "mango" ], "price" : [ { "orange" :
60, "jackfruit" : 200, "mango" : 120 } ] }
{ "_id" : ObjectId("5d716e876167c454bdec2905"), "fruits" : [ "pineapple", "strawberry", "grapes" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2906"), "fruits" : [ "banana", "strawbeery", "grapes" ] }
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "apple" ] }
>
Here second document is updated with price
addToSet
To update the document with "apple" at index 1 and push(addToSet) new value “jamun”in to the
"fruits" array.
Before:
> db.food.find({"fruits.1":"apple"})
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "apple", "cherry" ] }
Process:
> db.food.update({"fruits.1":"apple"},{$addToSet:{fruits:"jamun"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Result:
> db.food.find({"fruits.1":"apple"})
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "apple", "cherry", "jamun" ] }
>
pop
To update the document with "apple" at index 1 by popping an element from the list of elements present in
the array "fruits". The element popped is the one from the end of the array.
Before:
> db.food.find({"fruits.1":"apple"})
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "apple", "cherry", "jamun" ] }
Task:
> db.food.update({"fruits.1":"apple"},{$pop:{fruits:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Result:
> db.food.find({"fruits.1":"apple"})
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "apple", "cherry" ] }
>
Here from the array “jamun” is popped up here fruits:1 indicate last element in list
To update the document with "apple" at index 1 by popping an element from the list of elements present in
the array "fruits". The element popped is the one from the beginning of the array.
Before:
> db.food.find({"fruits.1":"apple"})
{ "_id" : ObjectId("5d716e876167c454bdec2907"), "fruits" : [ "orange", "apple", "cherry" ] }
Task:
> db.food.update({"fruits.1":"apple"},{$pop:{fruits:-1}})
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #50
pull
To update the documents having "banana" as an element in the array "fruits" and pop out the element
"banana" from those documents.
Before:
> db.food.find({"fruits.0":"banana"})
{ "_id" : ObjectId("5d716e876167c454bdec2906"), "fruits" : [ "banana" ] }
{ "_id" : ObjectId("5d7454259233b0160ec69414"), "fruits" : [ "banana", "apple", "cherry" ] }
{ "_id" : ObjectId("5d7454259233b0160ec69415"), "fruits" : [ "banana", "strawberry", "grapes" ] }
Task:
> db.food.update({"fruits.0":"banana"},{$pull:{fruits:"banana"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Result:
> db.food.find({"fruits.0":"banana"})
{ "_id" : ObjectId("5d7454259233b0160ec69414"), "fruits" : [ "banana", "apple", "cherry" ] }
{ "_id" : ObjectId("5d7454259233b0160ec69415"), "fruits" : [ "banana", "strawberry", "grapes" ] }
>
To update the document with "banana" by popping two elements from the list of elements present in the
array "fruits". The elements popped are "apple" and "cherry".
Before:
> db.food.find({"fruits.0":"banana"})
{ "_id" : ObjectId("5d7454259233b0160ec69414"), "fruits" : [ "banana", "apple", "cherry" ] }
{ "_id" : ObjectId("5d7454259233b0160ec69415"), "fruits" : [ "banana", "strawberry", "grapes" ] }
Task:
> db.food.update({"fruits.0":"banana"},{$pullAll:{fruits:["apple","cherry"]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Result:
> db.food.find({"fruits.0":"banana"})
{ "_id" : ObjectId("5d7454259233b0160ec69414"), "fruits" : [ "banana" ] }
{ "_id" : ObjectId("5d7454259233b0160ec69415"), "fruits" : [ "banana", "strawberry", "grapes" ] }
>
Here in the search found at first occurrence of criteria will be considered, not the other
Before:
>db.food.find({"fruits.0":"banana"})
{ "_id" : ObjectId("5d7454259233b0160ec69414"), "fruits" : [ "banana" ] }
{ "_id" : ObjectId("5d7454259233b0160ec69415"), "fruits" : [ "banana", "strawberry", "grapes" ] }
Step-1
Action: We set the element to null by index - "fruits.1":null
> db.food.updateMany({"fruits.0":"banana"},{$unset:{"fruits.1":null}})
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 1 }
intemediate result:
> db.food.find({"fruits.0":"banana"})
{ "_id" : ObjectId("5d7454259233b0160ec69414"), "fruits" : [ "banana" ] }
{ "_id" : ObjectId("5d7454259233b0160ec69415"), "fruits" : [ "banana", null, "grapes" ] }
Step-2: delete element whose value is null by pull command
> db.food.updateMany({"fruits.0":"banana"},{$pull:{fruits:null}})
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 1 }
Result:
> db.food.find({"fruits.0":"banana"})
{ "_id" : ObjectId("5d7454259233b0160ec69414"), "fruits" : [ "banana" ] }
{ "_id" : ObjectId("5d7454259233b0160ec69415"), "fruits" : [ "banana", "grapes" ] }
>
Aggregate Function
What is MongoDB Aggregation?
In aggregation operation, MongoDB processes the data records and returns a single computed result. It actually
groups multiple documents and then performs aggregation operation on it and after that returns a single result to
the end user.
i. Aggregation Pipeline
ii. Map-Reduce Function
iii. Single Purpose Aggregation Method
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #52
Aggregation process in MongoDB is modeled on the concept of data processing pipelines. Multiple
documents enter the pipeline and then these documents are being transformed into aggregated results.
The operations being performed during pipeline include filter and document transformation in which they
operate like queries and modify the form of output document respectively.
The pipeline method provides efficient data aggregation using other operations present in MongoDB. It can also
operate on a sharded collection.
ii. Map-Reduce
As the name Map-Reduce suggests it has two operations. Map operation, each document is being
processed along with emitting one or more objects for each document and reduce phase in which output of
map operation are being combined together.
MongoDB Map-reduce can optionally have a finalize stage in which it can do some final modifications in the
output of the document. Map-Reduce uses JavaScript to perform its operations including the finalize
operation.
"nRemoved" : 0,
"upserted" : [ ]
})
> show collections
customer
employee
food
student
> db.customer.find()
{ "_id" : ObjectId("5d745da59233b0160ec69416"), "custId" : "C101", "name" : "Murali", "accType" : "S", "balance" :
10200 }
{ "_id" : ObjectId("5d745da59233b0160ec69417"), "custId" : "C102", "name" : "Krishna", "accType" : "S", "balance" :
15204 }
{ "_id" : ObjectId("5d745da59233b0160ec69418"), "custId" : "C103", "name" : "Soumya", "accType" : "S", "balance" :
13092 }
{ "_id" : ObjectId("5d745da59233b0160ec69419"), "custId" : "C104", "name" : "Sailaja", "accType" : "S", "balance" :
110400 }
{ "_id" : ObjectId("5d745da59233b0160ec6941a"), "custId" : "C105", "name" : "Anamika Textiles", "accType" : "C",
"balance" : 1022200 }
>
Objective: Consider the collection "Customers" as given above. It has five documents.
Task: We would like to filter out those documents where the "AccType" has a value other than "S". After the
filter, We should be left with four documents where the "Acctype": "S". It is then required to group the
documents on the basis of CustID and sum up the "AccBal" for each unique "CustID". This is similar to the
output received with group by clause in RDBMS. Filter and display that group where the "TotAccBal" column
has a value greater than 15000.
{
"_id" : ObjectId("5d745da59233b0160ec69416"), {
"custId" : "C101", "_id":ObjectId("5d745da59233b0160ec69
"name" : "Murali", 416"),
"accType" : "S", "custId" : "C101",
"balance" : 10200 "name" : "Murali",
} "accType" : "S",
{ "balance" : 10200
"_id" : ObjectId("5d745da59233b0160ec69417"), }
"custId" : "C102", { {
"name" : "Krishna", "_id":ObjectId("5d745da59233b0160ec69 "_id" : ObjectId("5d745da59233b0160ec69417"),
"accType" : "S", 417"), "custId" : "C102",
"balance" : 15204 "custId" : "C102", "name" : "Krishna",
} "name" : "Krishna", "accType" : "S",
{ "accType" : "S", "balance" : 15204
"_id" : ObjectId("5d745da59233b0160ec69418"), "balance" : 15204 }
"custId" : "C103", } {
"name" : "Soumya", { "_id" : ObjectId("5d745da59233b0160ec69419"),
"accType" : "S", "_id":ObjectId("5d745da59233b0160ec69 "custId" : "C104",
"balance" : 13092 418"), "name" : "Sailaja",
} "custId" : "C103", "accType" : "S",
{ "name" : "Soumya", "balance" : 110400
"_id" : ObjectId("5d745da59233b0160ec69419"), "accType" : "S", }
"custId" : "C104", "balance" : 13092
"name" : "Sailaja", }
"accType" : "S", {
"balance" : 110400 "_id":ObjectId("5d745da59233b0160ec69
} 419"),
{ "custId" : "C104",
"_id" : ObjectId("5d745da59233b0160ec6941a"), "name" : "Sailaja",
"custId" : "C105", "accType" : "S",
"name" : "Anamika Textiles", "balance" : 110400
"accType" : "C", }
"balance" : 1022200
}
>
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #56
i. $project
ii. $match
iii. $group
iv. $sort
v. $skip & $limit
vi. $first & $last
vii. $unwind
i. $project :
This stage is used to select certain fields from a collection. We can also add, remove or reshape a key.
Syntax: { $project: { <specifications> } }
Specification Description
<field>: <1 or true> Specify the inclusion of a field.
_id: <0 or false> Specify the suppression of the _id field.
<field>: <expression> Add a new field or reset the value of an existing field.
List selected fields in documents from customer collection, By default ObjectId is displayed
> db.customer.aggregate( [ { $project : { custId:1,name:1 } } ] );
{ "_id" : ObjectId("5d745da59233b0160ec69416"), "custId" : "C101", "name" : "Murali" }
{ "_id" : ObjectId("5d745da59233b0160ec69417"), "custId" : "C102", "name" : "Krishna" }
{ "_id" : ObjectId("5d745da59233b0160ec69418"), "custId" : "C103", "name" : "Soumya" }
{ "_id" : ObjectId("5d745da59233b0160ec69419"), "custId" : "C104", "name" : "Sailaja" }
{ "_id" : ObjectId("5d745da59233b0160ec6941a"), "custId" : "C105", "name" : "Anamika Textiles" }
>
Here we assign the labels such as AccountNo, Customername for the fields as we wish for our convenience
Example –
db.example.aggregate([
{
$project:{
_id:1,
'dept':{$toUpper:'$name'},
'newexp':{$add:['$exp',10]}
}
}
])
Here we are creating ‘dept’ from earlier ‘name’ which was in upper case. And in ‘newexp’ we are adding the
experience as 10 years.
ii. $match
The MongoDB $match operator filters the documents to pass only those documents that match the specified
condition(s) to the next pipeline stage.
It is very much effective if you place the $match as early as possible in the aggregation pipeline that limits the
total number of documents in the aggregation pipeline.
When $match placed at the very beginning of a pipeline, the query can take advantage of indexes.
Note: The $where can not be used in $match queries as part of the aggregation pipeline
> db.customer.aggregate([{$match:{accType:"S"}}]);
{ "_id" : ObjectId("5d745da59233b0160ec69416"), "custId" : "C101", "name" : "Murali", "accType" : "S",
"balance" : 10200 }
{ "_id" : ObjectId("5d745da59233b0160ec69417"), "custId" : "C102", "name" : "Krishna", "accType" : "S",
"balance" : 15204 }
{ "_id" : ObjectId("5d745da59233b0160ec69418"), "custId" : "C103", "name" : "Soumya", "accType" : "S",
"balance" : 13092 }
{ "_id" : ObjectId("5d745da59233b0160ec69419"), "custId" : "C104", "name" : "Sailaja", "accType" : "S",
"balance" : 110400 }
>
In the above example we are aggregating documents that have a customer accType=”S”
> db.customer.aggregate([{$match:{accType:"C"}}]);
{ "_id" : ObjectId("5d745da59233b0160ec6941a"), "custId" : "C105", "name" : "Anamika Textiles",
"accType" : "C", "balance" : 1022200 }
>
Advanced example
Count the number of customers who have balance > 15000
> db.customer.aggregate([{$match:{balance:{$gt:15000}}},{$group:{_id:0,count:{$sum:1}}}]);
{ "_id" : 0, "count" : 3 }
>
Example –
db.example.aggregate([
{
$match:{
name:'xyz'
}
}
])
Here we are aggregating documents that have a name equal to xyz.
iii. $group
It does the work as the name says. It groups all documents based on some keys.
> db.customer.aggregate([{$group:{_id:null,count:{$sum:1}}}]);
{ "_id" : null, "count" : 5 }
>
In this example we count the number of documents
No we try to count the customers whose balance >15000 /Total number of customers / acccount Type is “S”
db.customer.aggregate([{$match:{balance:{$gt:15000}}},{$group:{_id:null,count:{$sum:1}}}]);
{ "_id" : null, "count" : 3 }
> db.customer.aggregate([{$group:{_id:null,count:{$sum:1}}}]);
{ "_id" : null, "count" : 5 }
> db.customer.aggregate([{$match:{accType:"S"}},{$group:{_id:null,count:{$sum:1}}}]);
{ "_id" : null, "count" : 4 }
in the above example total balance in the account Type “S” - savings accont
in the above example _id:”$accType” is the vlaue to be displayed
Task-3 - Total balance in account type = “C”
> db.customer.aggregate([{$match:{accType:"C"}},{$group:{_id:"$accType",TotBalance:
{$sum:"$balance"}}}]);
{ "_id" : "C", "TotBalance" : 1022200 }
>
in the above example total balance in the account Type “C” - current account
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #59
Example –
db.example.aggregate([
{
$group:{
_id:{'dept':'$name'},
employee_count:{$sum:10}
}
}
])
iv. $sort
Example –
Here we want to group all department in ascending order and then find the count of employees.
Task-1
> db.customer.aggregate([{$group:{_id:"$accType",TotBalance:{$sum:"$balance"}}},{$sort:{_id:1}}]);
{ "_id" : "C", "TotBalance" : 1022200 }
{ "_id" : "S", "TotBalance" : 148896 }
Task-2
> db.customer.aggregate([{$group:{_id:"$accType",TotBalance:{$sum:"$balance"}}},{$sort:{_id:-1}}]);
{ "_id" : "S", "TotBalance" : 148896 }
{ "_id" : "C", "TotBalance" : 1022200 }
>
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #60
db.example.aggregate([
{
$group:{
_id:'$name',
employee_count:{$sum:1}
}
},
{
$sort:{
_id:1
}
}
])
Using skip we can skip forward in the list of all documents for the given limit.
Using limit we can limit the number of documents we want to look at by specifying the limit as per our
convenience.
Example –
db.example.aggregate([
{
$group:{
_id:'$name',
employee_count:{$sum:10}
}
},
{
$sort:{
_id:1
}
},
{
$skip:4
},
{
$limit:5
}
])
It is used to get the first and last values in each group of documents.
Example –
db.example.aggregate([
{
$group:{
_id:'$name',
employee_count:{$sum:1},
record:{ $first:'$code'}
}
}
])
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #61
vii. $unwind
We can use unwind for all documents, that are using arrays.
Example –
{
a:abcdata,
b:xyzdata,
c:[a1,a2,a3]
}
After this if we perform unwind operation on c then we will get 3 documents as follows:
{
a:abcdata,
b:xyzdata,
c:a1
}
{
a:abcdata,
b:xyzdata,
c:a2
}
{
a:abcdata,
b:xyzdata,
c:a3
}
****
Map function
The map function will call the emit function one or more times (above diagram). We can access all the
attributes of each document in the collection with the this keyword.
The intermediate hash-map contains only unique keys, so if the emit function sends a key that is already in
the hash-map, the value is going to be inserted in a list of values.
Each record in the hash-map will look like this: key:One, value:[1,2,3,...] .
Here, we can see a sample code of the map function:
function(){
emit(this._id, {count: 1});
}
Reduce function
The reduce function will receive two arguments: the key and values (one or a list of values). This function is
called for each record in the hash-map.
In the following code, we can see a sample reduce function. In this case, the function returns the total count for
each key:
function(key, values) {
total = 0;
for (var i = 0; i < values.length; ++i) {
total += values[i].count;
};
return {count: total};
}
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #63
The above map-reduce function will query the collection, and then map
db.collection.mapReduce the output documents to the emit key – value pairs. After this, it is reduced
( based on the keys that have multiple values.
function() {emit(key, value);}, Map: It is a JavaScript function. It is used to map a value with a key and
//Define map function produces a key-value pair.
"_id" : ObjectId("5d75144647bf7030afa9a26f"),
"book_title" : "Parallel Computing",
"author_name" : "rajaraman",
"status" : "active",
"publish_year" : "2014"
}
{
"_id" : ObjectId("5d75147b47bf7030afa9a270"),
"book_title" : "Fundamentals of Computers",
"author_name" : "rajaraman",
"status" : "passive",
"publish_year" : "2010"
}
>
Now, we are going to use the mapReduce function on our author collection in order to select all the active
books, group them together on the basis of author_name and then count the number of books by each
author by using the following code in MongoDB.
Output:
> db.author_total.find();
{ "_id" : "aparajita", "value" : 2 }
{ "_id" : "rajaraman", "value" : 1 }
{ "_id" : "keethika", "value" : 1 }
>
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #65
> db.author_total.find();
{ "_id" : "rajaraman", "value" : 1 }
{ "_id" : "aparajita", "value" : 1 }
>
db.system.js.find();
As per the screenshot above, currently there are no functions in the system.js collection.
Act:
db.system.js.insert({_id:"factorial",
value:function (n) {
if (n==1) return 1; else
return n * factorial(n-1);
}
});
'Note: "Alphabets" is the name of the collection and "alphabet" is the name of the field.(key)
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #66
>
db.alphabets.find();
{ "_id" : 1, "alphabet" : "a" }
{ "_id" : 2, "alphabet" : "b" }
{ "_id" : 3, "alphabet" : "c" }
{ "_id" : 4, "alphabet" : "d" }
{ "_id" : 5, "alphabet" : "e" }
{ "_id" : 6, "alphabet" : "f" }
{ "_id" : 7, "alphabet" : "g" }
{ "_id" : 8, "alphabet" : "h" }
{ "_id" : 9, "alphabet" : "i" }
{ "_id" : 10, "alphabet" : "j" }
{ "_id" : 11, "alphabet" : "k" }
{ "_id" : 12, "alphabet" : "l" }
{ "_id" : 13, "alphabet" : "m" }
{ "_id" : 14, "alphabet" : "n" }
{ "_id" : 15, "alphabet" : "o" }
{ "_id" : 16, "alphabet" : "p" }
{ "_id" : 17, "alphabet" : "q" }
{ "_id" : 18, "alphabet" : "r" }
{ "_id" : 19, "alphabet" : "s" }
{ "_id" : 20, "alphabet" : "t" }
Type "it" for more
>
The db.collection.find() method returns a cursor. To access the documents, you need to iterate the cursor.
However, in the mongo shell, if the returned cursor is not assigned to a variable using the var keyword, then the
cursor is automatically iterated up to 20 times to print up to the first 20 documents in the results.
The following examples describe ways to manually iterate the cursor to access the documents or to use the
iterator index.
Manually Iterate the Cursor
In the mongo shell, when you assign the cursor returned from the find() method to a variable using the var
keyword, the cursor does not automatically iterate.
You can call the cursor variable in the shell to iterate up to 20 times and print the matching documents, as in the
following example:
Let us now look at designing manual cursors to iterate through me documents in the "alphabets" collection. We
will use two methods with manual cursors:
1. hasNext()
2. next().
Method 1: hasNext() method. Return value: Boolean. The hasNext() method returns true if the cursor
returned by the db.Collection.find() query can iterate further to return more documents.
Method 2: next() method. The next() method returns the next document in the cursor as returned by the
db.collection.find() method.
MongoDB creates a unique index on the _id field during the creation of a collection.
The _id index prevents clients from inserting two documents with the same value for the _id field. You
cannot drop this index on the _id field.
Create an Index
To create an index in the Mongo Shell, use db.collection.createIndex().
db.collection.createIndex( <key and index type specification>, <options> )
The following example creates a single key descending index on the name field:
db.collection.createIndex({ name: -1 })
The db.collection.createIndex method only creates an index if an index of the same specification does not
already exist.
Note: MongoDB indexes use a B-tree data structure.
Index Names
The default name for an index is the concatenation of the indexed keys and each key’s direction in the
index ( i.e. 1 or -1) using underscores as a separator.
For example, an index created on { item : 1, quantity: -1 } has the name item_1_quantity_-1.
You can create indexes with a custom name, such as one that is more human-readable than the default.
For example, consider an application that frequently queries the products collection to populate data on existing
inventory.
The following createIndex() method creates an index on item and quantity named query for inventory:
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #71
db.products.createIndex(
{ item: 1, quantity: -1 } ,
{ name: "query for inventory" }
)
Index Types
MongoDB provides a number of different index types to support specific types of data and queries.
a. Single Field
b. Compound Index
c. Multikey Index
d. Geospatial Index
e. Text Indexes
f. Hashed Indexes
a. Single Field :
In addition to the MongoDB-defined _id index, MongoDB supports the creation of user-defined
ascending/descending indexes on a single field of a document.
For a single-field index and sort operations, the sort order (i.e. ascending or descending) of the index key
does not matter because MongoDB can traverse the index in either direction.
b. Compound Index
MongoDB also supports user-defined indexes on multiple fields, i.e. compound indexes.
The order of fields listed in a compound index has significance.
For instance, if a compound index consists of { userid: 1, score: -1 }, the index sorts first by userid and then,
within each userid value, sorts by score.
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #72
For compound indexes and sort operations, the sort order (i.e. ascending or descending) of the index keys can
determine whether the index can support a sort operation.
c. Multikey Index
MongoDB uses multikey indexes to index the content stored in arrays. If you index a field that holds an
array value, MongoDB creates separate index entries for every element of the array. These multikey
indexes allow queries to select documents that contain arrays by matching on element or elements of the arrays.
MongoDB automatically determines whether to create a multikey index if the indexed field contains an
array value; you do not need to explicitly specify the multikey type.
d. Geo-spatial Index
To support efficient queries of geospatial coordinate data, MongoDB provides two special indexes:
(i) 2d indexes that uses planar geometry when returning results
(ii) 2d sphere indexes that use spherical geometry to return results.
e. Text Indexes
MongoDB provides a text index type that supports searching for string content in a collection. These text
indexes do not store language-specific stop words (e.g. “the”, “a”, “or”) and stem the words in a collection
to only store root words.
f. Hashed Indexes
To support hash based sharding, MongoDB provides a hashed index type, which indexes the hash of the
value of a field. These indexes have a more random distribution of values along their range, but only support
equality matches and cannot support range-based queries.
6.5.14 MongoImport
MongoImport used at the command prompt imports CSV (Comma Separated Values) or TSV (Tab Separated
Values) files or JSON (Java Script Object Notation) documents into MongoDB.
(in Ubuntu - $ prompt / Windows - command prompt)
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #73
To import data, open a new Terminal/Command Prompt window and enter mongoimport followed by
parameters such as database name, collection name, source file name, etc.
You might remember that we previously used mongoexport to export the artists collection to a JSON file.
Now, let's switch back to our mongo Terminal/Command Prompt window and retrieve the list of collections in
our database:
>show collections
Result:
artists
musicians
producers
db.artists.find()
Result:
You can use the --collection argument to provide a name of the collection that the data should go into.
Let's import another file, but this time, specify a collection name:
Resulting message:
>show collections
Resulting message:
artists
jazz
musicians
producers
And finally, query the jazz collection:
>db.jazz.find().pretty()
Resulting message:
{
"_id" : ObjectId("578214f048ef8c6b3ffb0159"),
"artistname" : "Miles Davis",
"albums" : [
{
"album" : "Kind of Blue",
"year" : 1959,
"genre" : "Jazz"
},
{
"album" : "Bitches Brew",
"year" : 1970,
"genre" : "Jazz"
}
]
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #75
if the CSV file has a header row, use --headerline to tell mongoimport to use the first line to determine the
name of the fields in the resulting document.
If the CSV file doesn't have a header row, use the --fields parameter to set the field names.
_id,albumname,artistname
1,Killers,"Iron Maiden"
2,Powerslave,"Iron Maiden"
12,"Somewhere in Time","Iron Maiden"
3,"Surfing with the Alien","Joe Satriani"
10,"Flying in a Blue Dream","Joe Satriani"
11,"Black Swans and Wormhole Wizards","Joe Satriani"
6,"Out of the Loop","Mr Percival"
7,"Suck on This",Primus
8,"Pork Soda",Primus
9,"Sailing the Seas of Cheese",Primus
> db.catalog.find()
{ "_id" : 2, "albumname" : "Powerslave", "artistname" : "Iron Maiden" }
{ "_id" : 1, "albumname" : "Killers", "artistname" : "Iron Maiden" }
{ "_id" : 3, "albumname" : "Surfing with the Alien", "artistname" : "Joe Satriani" }
{ "_id" : 12, "albumname" : "Somewhere in Time", "artistname" : "Iron Maiden" }
{ "_id" : 10, "albumname" : "Flying in a Blue Dream", "artistname" : "Joe Satriani" }
{ "_id" : 6, "albumname" : "Out of the Loop", "artistname" : "Mr Percival" }
{ "_id" : 7, "albumname" : "Suck on This", "artistname" : "Primus" }
{ "_id" : 8, "albumname" : "Pork Soda", "artistname" : "Primus" }
{ "_id" : 11, "albumname" : "Black Swans and Wormhole Wizards", "artistname" : "Joe Satriani" }
{ "_id" : 9, "albumname" : "Sailing the Seas of Cheese", "artistname" : "Primus" }
Here's another CSV file, but this one doesn't have a header row:
Mutt Lange, 1948
John Petrucci, 1967
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #76
DJ Shadow, 1972
George Clinton, 1941
> mongoimport --db mydb --collection producers --type csv --fields name,born –file
/home/hduser/17207/producers.csv
Here we are specifying the fields name,born to the collection during import
Query the collection:
> db.producers.find()
{ "_id" : 1, "name" : "Bob Rock" }
{ "_id" : ObjectId("5784a3a5dfad478c015f6b72"), "name" : "John Petrucci", "born" : 1967 }
{ "_id" : ObjectId("5784a3a5dfad478c015f6b73"), "name" : "Mutt Lange", "born" : 1948 }
{ "_id" : ObjectId("5784a3a5dfad478c015f6b74"), "name" : "George Clinton", "born" : 1941 }
{ "_id" : ObjectId("5784a3a5dfad478c015f6b75"), "name" : "DJ Shadow", "born" : 1972 }
You'll see that the ObjectId field has been automatically created and populated for us.
Also, we already had one document in this collection before we ran the import: {"_id" : 1,"name":"Bob Rock"}.
Therefore, you can see that the import has simply added to the collection (as opposed to replacing it and all its
contents).
6.5.15 MongoExport
MongoExport used at the command prompt exports MongoDB JSON documents into CSV (Comma
Separated Values) or TSV (Tab Separated Values) files or JSON (Java Script Object Notation) documents.
In MongoDB, you can export data using the mongoexport utility.
You can use the mongoexport utility to export data from your MongoDB database, to a JSON or CSV file.
The utility is located in the MongoDB bin directory (eg, /mongodb/bin). When you run the utility, provide
the name of the database, the collection, and the file you want it to be exported to.
To export data, first open a new Terminal/Command Prompt window, then type the applicable command.
Resulting file:
If you find that you can't run mongoexport, be sure that you've either exited the mongo utility,
or
opened a new Terminal/Command Prompt window before running mongoexport, as it is a separate utility.
The above command assumes that the MongoDB bin directory is in your PATH. If it's not, you will need to use
the full path to the mongoexport file.
For example, /mongodb/bin/mongoexport or wherever your MongoDB deployment is installed.
If you don't provide a file path for the exported file, it will be created wherever you are located when you run
the command. Either provide the full path, or navigate to where you'd like the data file to be written before you
run the command.
You must also specify the fields in the MongoDB documents to export.
Here, we use mongoexport to export the artists collection to a CSV file. We export the _id and artistname fields.
We've also given the file name a .csv extension.
$mongoexport --db mydb --collection artists --type=csv --fields _id,artistname --out /home/hduser/17207/artists.csv
Resulting message:
_id,artistname
ObjectId(5780fbf948ef8c6b3ffb0149),The Tea Party
ObjectId(5781c9ac48ef8c6b3ffb014a),Jorn Lande
1,AC/DC
ObjectId(5781d7f248ef8c6b3ffb014d),The Kooks
ObjectId(5781d7f248ef8c6b3ffb014e),Bastille
ObjectId(5781d7f248ef8c6b3ffb014f),Gang of Four
ObjectId(5781f85d48ef8c6b3ffb0150),Deep Purple
ObjectId(578214f048ef8c6b3ffb0159),Miles Davis
ObjectId(578217c248ef8c6b3ffb015a),Robben Ford
ObjectId(578217c248ef8c6b3ffb015b),Snoop Dogg
2,Prince
3,Moby
4,Rush
You can use the --query option to specify a query to export. The query must be enclosed in single quotes.
$ mongoexport --db mydb --collection artists --query '{"artistname": "Miles Davis"}' --out
home/hduser/17207/miles_davis.json
Resulting message:
{"_id":{"$oid":"578214f048ef8c6b3ffb0159"},"artistname":"Miles Davis","albums":[{"album":"Kind of
Blue","year":1959.0,"genre":"Jazz"},{"album":"Bitches Brew","year":1970.0,"genre":"Jazz"}]}
Other Options
The mongoexport utility provides a number of options. Here are some potentially useful ones.
{"_id":1.0,"artistname":"AC/DC"}
Here, we sort the file by the _id field in ascending order (i.e. 1). To make it descending, use a -1.
mongoexport --db mydb --collection artists --limit 3 --sort '{_id: 1}' --out
/home/hduser/17207/3_artists_sorted.json
Resulting file:
{"_id":1.0,"artistname":"AC/DC"}
{"_id":2.0,"artistname":"Prince","address":{"street":"Audubon
Road","city":"Chanhassen","state":"Minnesota","country":"United States"}}
{"_id":3.0,"artistname":"Moby","albums":[{"album":"Play","year":1999.0,"genre":"Electronica"},
{"album":"Long Ambients 1: Calm. Sleep.","year":2016.0,"genre":"Ambient"}]}
Allows you to instruct mongoexport to skip a number of documents before starting the export operation.
mongoexport --db mydb --collection artists --limit 3 --sort '{_id: 1}' --skip 2 --out
/home/hduser/17207/3_artists_sorted_skipped.json
Resulting file:
{"_id":3.0,"artistname":"Moby","albums":[{"album":"Play","year":1999.0,"genre":"Electronica"},
{"album":"Long Ambients 1: Calm. Sleep.","year":2016.0,"genre":"Ambient"}]}
{"_id":4.0,"artistname":"Rush"}
{"_id":{"$oid":"5780fbf948ef8c6b3ffb0149"},"artistname":"The Tea Party"}
$mongoexport --db mydb --collection artists --query '{"artistname": "Miles Davis"}' --pretty --out
/home/hduser/17207/miles_davis_pretty.json
Resulting file:
{
"_id": {
"$oid": "578214f048ef8c6b3ffb0159"
},
"artistname": "Miles Davis",
"albums": [
{
"album": "Kind of Blue",
"year": 1959.0,
"genre": "Jazz"
},
{
MCA V – CA5T1 - DATA SCIENCES – UNIT- III – MongoDB #80
Step 1: Run the insert () method on a new collection "usercounters". This is to start off with an initial value of 0
for the "seq" field.
db.usercounters.insert( {
_id: "empid",
seq:0 }
)
Step 2: Create a user-defined function "getnextseq". This method will invoke "findAndModify()" method on
the "usercounters" collection. This is to increment the value of seq field by 1 and update the same in
"usercounters" collection.
function getnextseq(name) {
var ret=db.usercounters. findAndModify ( {
query: {_id:name}, update: {$inc:{seq:l}},
new: true }
);
return ret.seq;
}
Step 3: Run the insert() method on the collection where you need to have the "_id" field and get the uniquely
generated number. Notice the call to getnextseq() method as value to _id. The return value from the
getnextseq() method becomes the value of _id.
db.users.insert( {
_id:getnextseq(Mempid"),
Name: "sarah jane" }
)