Introduction
MongoDB is very powerful
NoSQL database.
Points to Remember
1)A document is the
basic unit of data for MongoDB and is roughly equivalent to a
row in a relational
database management system
2)a collection can be
thought of as a table with a dynamic schema.
3)A single instance of
MongoDB can host multiple independent databases, each of
which can have its own
collections.
4)Every document has a
special key, "_id", that is unique within a collection.
5)MongoDB comes with a
simple but powerful JavaScript shell, which is useful for
the administration of
MongoDB instances and data manipulation.
There are also
three reserved database names which you can access but which have
special semantics. These
are as follows
admin
This is the “root”
database, in terms of authentication. If a user is added to the admin
database, the user
automatically inherits permissions for all databases. There are
also certain server-wide
commands that can be run only from the admin database,
such as listing all of
the databases or shutting down the server
local
This database will never
be replicated and can be used to store any collections that
should be local to a
single server
config
When MongoDB is being
used in a sharded setup it uses the config database to store information about
the shards.
Lets start with CRUD
Operation with MongoDB
1)Insert document
db.foo.insert({"bar"
: "baz"})
2)Remove document
db.foo.remove({})
It will remove all
documents from MongoDB.
Instead of removing
document use drop to delete all the documents
db.foo.drop()
db.foo.remove({"opt-out"
: true})
It will remove documents
that matched criteria with specific conditions
3)Update
3.1)Document Replacement
The simplest type of
update fully replaces a matching document with a new one.This can be
useful to do a dramatic schema migration
foo collection document
{
"_id" :
ObjectId("4b2b9f67a1f631733d917a7a"),
"name" :
"joe",
"friends" :
32,
"enemies" : 2
}
output document
db.foo.update({"name":"joe"},{"test":5})
{
"_id" :
ObjectId("4b2b9f67a1f631733d917a7a"),
"test":5
}
3.2)Partial Document
Update
It is generally used in
the scenario for the update document
3.2.1)Using $inc
Operator
db.counters.insert({"_id"
: ObjectId("4b253b067525f35f94b60a31"),
"url" :
"www.example.com","pageviews" : 52})
To increment the value
of "pageviews" field.
db.counters.update({"url":"www.example.com"},{"$inc"
: {"pageviews" : 1}})
To decrement the value
of "pageviews" field.
db.counters.update({"url":"www.example.com"},{"$inc"
: {"pageviews" : -1}})
3.2.2)Using $set
Modifier
First insert following
record
db.users.insert(
{
"name" :
"joe",
"age" : 30,
"sex" :
"male",
"location" :
"Wisconsin",
"favorite
book" : "War and Peace"
})
Update Particular Field
using $set
db.users.update({"name":"joe"},{$set:{"favorite
book":"Green Eggs and Ham"}});
Output Document
{
"name" : "joe",
"age" : 30,
"sex" : "male",
"location" : "Wisconsin",
"favorite book" : "Green Eggs and Ham"
}
Now I am updating
"favorite book" as an array.
db.users.update({"name"
: "joe"}, {"$set" : {"favorite book" :
["Cat's Cradle", "Foundation Trilogy", "Ender's
Game"]}})
Output Document
{
"_id" : ObjectId("58594ec581c3fb597fff7a1e"),
"name" : "joe",
"age" : 30,
"sex" : "male",
"location" : "Wisconsin",
"favorite book" : [
"Cat's Cradle",
"Foundation Trilogy",
"Ender's Game"
]
}
To Remove Particular
field
db.users.update({"name"
: "joe"},{"$unset" : {"favorite book" : 1}})
You can use $set to
Modify Embedded Document as well
db.blog.posts.insert({"title"
: "A Blog Post","content" : "Test Post",
"author" :
{"name" : "joe","email" :
"joe@example.com"}})
Update the author name
in Embedded Document
db.blog.posts.update({"author.name"
: "joe"},{"$set" : {"author.name" : "joe
schmoe"}})
Output
{
"_id" : ObjectId("585b68b51184acf49fd5a26b"),
"title" : "A Blog Post",
"content" : "Test Post",
"author" : {
"name" : "joe schmoe",
"email" :
"joe@example.com"
}
}
Remove author field
db.blog.posts.update({},{"$unset"
: {"author" : 1}})
Modify your document
using Arrays
Using $push to you can
add item in Array.
db.blog.posts.update({},
{"$push" :
{"comments" : {"name" : "joe", "email"
: "joe@example.com", "content" : "nice post."}}})
Output
{
"_id" : ObjectId("585b68b51184acf49fd5a26b"),
"title" : "A Blog Post",
"content" : "Test Post",
"comments" : [
{
"name"
: "joe",
"email" : "joe@example.com",
"content" : "nice post."
}
]
}
Add another item in
Array
db.blog.posts.update({},
{"$push" :
{"comments" : {"name" : "bob", "email"
: "bob@example.com", "content" : "first comment form
bob"}}})
Output
{
"_id" : ObjectId("585b68b51184acf49fd5a26b"),
"title" : "A Blog Post",
"content" : "Test Post",
"comments" : [
{
"name"
: "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name"
: "bob",
"email" : "bob@example.com",
"content"
: "first comment form bob"
}
]
}
Add another item in
Array
db.blog.posts.update({},
{"$push" :
{"comments" : {"name" : "ritesh",
"email" : "ritesh@example.com", "content" :
"first comment form ritesh patel legend"}}})
Output
{
"_id" : ObjectId("585b68b51184acf49fd5a26b"),
"title" : "A Blog Post",
"content" : "Test Post",
"comments" : [
{
"name"
: "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name"
: "bob",
"email" : "bob@example.com",
"content"
: "first comment form bob"
},
{
"name"
: "ritesh",
"email" : "ritesh@example.com",
"content" : "first comment form ritesh patel legend"
}
]
}
Add multiple documents
at the same time
db.blog.posts.update({},
{"$push" :
{"comments" : {"$each" :
[{"name" :
"nilap", "email" : "nilap@example.com",
"content" : "first comment form Nilap Shah"},
{"name" :
"Gaurang", "email" : "Gaurang@example.com",
"content" : "first comment form Gaurang"},
{"name" :
"Jignesh", "email" : "Jignesh@example.com",
"content" : "first comment form Jignesh"}]}}})
Output
{
"_id" : ObjectId("585b68b51184acf49fd5a26b"),
"title" : "A Blog Post",
"content" : "Test Post",
"comments" : [
{
"name"
: "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name"
: "bob",
"email" : "bob@example.com",
"content" : "first comment form bob"
},
{
"name"
: "ritesh",
"email" : "ritesh@example.com",
"content" : "first comment form ritesh patel legend"
},
{
"name"
: "nilap",
"email" : "nilap@example.com",
"content" : "first comment form Nilap Shah"
},
{
"name"
: "Gaurang",
"email" : "Gaurang@example.com",
"content" : "first comment form Gaurang"
},
{
"name"
: "Jignesh",
"email" : "Jignesh@example.com",
"content" : "first comment form Jignesh"
}
]
}
Suppose we wanted to
store only last five items in the array
(Use $push,$each and
$slice in the document)
db.blog.posts.update({},
{"$push" :
{"comments" : {
"$each" : [
{"name" :
"Bharat", "email" : "bharat@example.com",
"content" : "first comment form bharat"},
{"name" :
"Dipali", "email" : "dipali@example.com",
"content" : "first comment form dipali"}],
"$slice" :
-5}}})
Output
{
"_id" : ObjectId("585b68b51184acf49fd5a26b"),
"title" : "A Blog Post",
"content" : "Test Post",
"comments" : [
{
"name"
: "nilap",
"email" : "nilap@example.com",
"content" : "first comment form Nilap Shah"
},
{
"name"
: "Gaurang",
"email" : "Gaurang@example.com",
"content" : "first comment form Gaurang"
},
{
"name"
: "Jignesh",
"email" : "Jignesh@example.com",
"content" : "first comment form Jignesh"
},
{
"name"
: "Bharat",
"email" : "bharat@example.com",
"content" : "first comment form bharat"
},
{
"name"
: "Dipali",
"email" : "dipali@example.com",
"content" : "first comment form dipali"
}
]
}
Note: if you wanted to
keep item based on sorting user $sort with $slice.
Suppose you wanted to modify any item in Array
db.blog.posts.update({"comments.email":"Jignesh@example.com"},{"$set"
: {"comments.$.email" : "Jignesh@cignex.com"}})
Output
{
"_id" :
ObjectId("585b68b51184acf49fd5a26b"),
"title" : "A Blog
Post",
"content" : "Test
Post",
"comments" : [
{
"name" :
"nilap",
"email" :
"nilap@example.com",
"content" :
"first comment form Nilap Shah"
},
{
"name" :
"Gaurang",
"email" :
"Gaurang@example.com",
"content" :
"first comment form Gaurang"
},
{
"name" :
"Jignesh",
"email" :
"Jignesh@cignex.com",
"content" :
"first comment form Jignesh"
},
{
"name" :
"Bharat",
"email" :
"bharat@example.com",
"content" :
"first comment form bharat"
},
{
"name" :
"Dipali",
"email" :
"dipali@example.com",
"content" :
"first comment form dipali"
}
]
}
$addToSet
It is same as $push but
it will check the item is exist or not. If not then it adds the item in the
array.
First insert one record
in the array
db.users.insert({"username"
: "joe","emails" :
["joe@example.com","joe@gmail.com","joe@yahoo.com"]})
Output:
{
"_id" :
ObjectId("585cf46e1184acf49fd5a26c"),
"username" : "joe",
"emails" : [
"joe@example.com",
"joe@gmail.com",
"joe@yahoo.com"
]
}
Now I am trying to add item
in email field.
db.users.update({"username"
: "joe"},
{"$addToSet" :
{"emails" : "joe@hotmail.com"}})
Output:
{
"_id" :
ObjectId("585cf46e1184acf49fd5a26c"),
"username" : "joe",
"emails" : [
"joe@example.com",
"joe@gmail.com",
"joe@yahoo.com",
"joe@hotmail.com"
]
}
Now I am trying to
insert the item which is already exist in the array.
db.users.update({"username"
: "joe"},
{"$addToSet" :
{"emails" : "joe@gmail.com "}})
Output:
{
"_id" :
ObjectId("585cf46e1184acf49fd5a26c"),
"username" : "joe",
"emails" : [
"joe@example.com",
"joe@gmail.com",
"joe@yahoo.com",
"joe@hotmail.com"
]
}
Removing elements from the array.
db.hobbies.insert({"todo"
: ["cricket", "pool",
"chess","cricket"]})
Output:
{
"_id" :
ObjectId("585cf6d81184acf49fd5a26d"),
"todo" : [
"cricket",
"pool",
"chess",
"cricket"
]
}
Using $pull operator
db.hobbies.update({},
{"$pull" : {"todo" : "cricket"}})
Output
{
"_id" :
ObjectId("585cf6d81184acf49fd5a26d"),
"todo" : [
"pool",
"chess"
]
}
Pulling will remove all the items from an array which matches the criteria.
Removing elements from an array using $pop Operator.
db.arr.insert({"testarr":[1,1,2,3,4,5,6,7]})
Output
{
"_id" : ObjectId("585cf9c11184acf49fd5a26e"),
"testarr" : [
1,
1,
2,
3,
4,
5,
6,
7
]
}
Removing element from last
db.arr.update({}, {"$pop" : {"testarr" : 1}})
Output:
{
"_id" : ObjectId("585cf9c11184acf49fd5a26e"),
"testarr" : [
1,
1,
2,
3,
4,
5,
6
]
}
Removing element from begining of array
db.arr.update({}, {"$pop" : {"testarr" : -1}})
Output:
{
"_id" : ObjectId("585cf9c11184acf49fd5a26e"),
"testarr" : [
1,
2,
3,
4,
5,
6
]
}