Command line mongo
#Mongo Shell
mongo
Switch to a database
use test
List all databases
show dbs
List all collections
show collections
db.getCollectionNames()
Insert to the test database
db.restaurants.insert(
{
"address" : {
"street" :
"2 Avenue",
"zipcode" :
"10075",
"building" :
"1480",
"coord" : [
-73.9557413, 40.7720266 ],
},
"borough" :
"Manhattan",
"cuisine" :
"Italian",
"grades" : [
{
"date" :
ISODate("2014-10-01T00:00:00Z"),
"grade" :
"A",
"score" :
11
},
{
"date" :
ISODate("2014-01-16T00:00:00Z"),
"grade" :
"B",
"score" :
17
}
],
"name" :
"Vella",
"restaurant_id" :
"41704620"
}
)
Query for all documents in a collection
db.restaurants.find()
Query with pretty
db.restaurants.find().pretty()
Query by a top level field
db.restaurants.find( { "borough":
"Manhattan" } )
Query by a field in an embedded document
db.restaurants.find( { "address.zipcode":
"10075" } )
Query by a field in an Array
db.restaurants.find( { "grades.grade":
"B" } )
Query by operators
db.restaurants.find( { "grades.score": { $gt: 30 }
} )
db.restaurants.find( { "grades.score": { $lt: 10 }
} )
Query by logical conditions
Logical AND
db.restaurants.find( { "cuisine":
"Italian", "address.zipcode": "10075" } )
Logical OR
db.restaurants.find(
{ $or: [ { "cuisine":
"Italian" }, { "address.zipcode": "10075" } ] }
)
Sort query results
# 1 for ascending, -1 for descending
db.restaurants.find().sort( { "borough": 1,
"address.zipcode": 1 } )
Update top level fields
db.restaurants.update(
{
"name" : "Vella" },
{
$set: {
"cuisine": "American (New)" },
$currentDate:
{ "lastModified": true }
}
)
Update an embedded field
db.restaurants.update(
{
"restaurant_id" : "41704620" },
{ $set: {
"address.street": "East 31st Street" } }
)
Update multiple documents
db.restaurants.update(
{
"address.zipcode": "10075", cuisine: "American
(New)" },
{
$set: { cuisine:
"Category To Be Determined" },
$currentDate: {
"lastModified": true }
},
{ multi: true}
)
Replace a document
db.restaurants.update(
{
"restaurant_id" : "41704620" },
{
"name" : "Vella 2",
"address" : {
"coord" : [ -73.9557413, 40.7720266 ],
"building" : "1480",
"street" : "2 Avenue",
"zipcode" : "10075"
}
}
)
Remove all documents that match a condition
db.restaurants.remove( { "name": "Vella
2" } )
Use the justOne option
#removes only one of the matching documents
db.restaurants.remove( { "borough":
"Manhattan" }, { justOne: true } )
Remove all documents
db.restaurants.remove( { } )
Drop a collection
db.restaurants.drop()
Data Aggregation
Group documents by a field and calculate count
# Use the $group
stage to group by a specified key. In the $group stage, specify the group by key in the _id field. $group accesses fields
by the field path, which is the field name prefixed by a dollar sign $.
The $group stage can use accumulators to perform calculations for each group.
The following example groups the documents in the restaurants collection by the
borough field and uses the $sum
accumulator to count the documents for each group.
db.restaurants.aggregate(
[
{ $group: {
"_id": "$borough", "count": { $sum: 1 } } }
]
);
Filter and group documents
#Use the $match
stage to filter documents. $match uses the MongoDB query syntax. The following
pipeline uses $match to query the restaurants collection for documents with borough equal to "Manhattan"
and cuisine equal to Italian. Then
the $group stage groups the matching
documents by the address.zipcode field and uses the $sum accumulator to calculate the count.
db.restaurants.aggregate(
[
{ $match: {
"borough": "Manhattan", "cuisine":
"Italian" } },
{ $group: {
"_id": "$address.zipcode" , "count": { $sum: 1 }
} }
]);
Indexes
Create a single field index
db.restaurants.createIndex( { "cuisine": 1 } )
Create a compound index
# MongoDB supports compound indexes which are indexes on
multiple fields. The order of the fields determine how the index stores its
keys. For example, the following operation creates a compound index on the
"cuisine" field and the "address.zipcode" field. The index
orders its entries first by ascending "cuisine" values, and then,
within each "cuisine", by descending "address.zipcode"
values.
db.restaurants.createIndex( { "cuisine": 1,
"address.zipcode": -1 } )
Unique constraint
#Just an example. Does not work with the restaurants
collection.
db.members.createIndex( { "user_id": 1 }, {
unique: true } )
FindOne
Find one document
#Find random document by calling findOne without any search
parameters
db.people.findOne()
Find one specific document
#Find specific document by passing in one or more key values
db.people.findOne( {
“name” : “Smith” } )
db.people.findOne( { “name” : “Smith” , “age” : 30 } )
Find one document and display only the fields you want to see
#Find specific document and specify the fields you want to
display. By default, _id is displayed.
db.people.findOne( { “name” : “Jones” } , { “name” : true , “_id” : false } )
Query using $gt and $lt
Get all documents where a numeric value is greater than or less than a value
# $gt greater than
# $lt lesser than
# $gte greater than or equal to
# $lte lesser than or equal to
#Query by single condition
db.scores.find( { “score” : { “$gt” : 95 } } )
#Query by condition and additional search values
db.scores.find( { “score” : { “$gt” : 95 }
, “type” : “essay” } )
#Query by multiple conditions
db.scores.find( { “score” : { “$gt” : 95 , “lte” : 98 } } )
Get all documents where a string is lexicographically greater than or less than another string
#Query for finding all documents where name is lexicographically
greater than or equal to the letter D
db.people.find( { “name” : { $gt : “D” } } )
#Query for finding all documents where name is
lexicographically greater than or equal to the letters Je
db.people.find( { “name” : { $gt : “Je” } } )
#Query for finding all documents where name is
lexicographically greater than or equal to the letters Je and less than Jo
db.people.find(
{ "name" : { $gte : "Je" , "$lt" : "Jo"
} } )
Exists
#Find all documents where the key profession exists
db.people.find( { “profession” : { $exists : true } } )
Type of a field
#Find all documents where name is a string value. $type of 2
is based on BSON specification.
db.people.find( { “name” : { $type : 2} }
Regular Expressions
#Query for string patterns
db.people.find(
{ "name" : { "$regex" : "A" } } )
#Query for a document where the string name ends in letter
‘y’
db.people.find(
{ "name" : { "$regex" : "y$" } } )
#Query for document where name begins with letter ‘A’
db.people.find(
{ "name" : { "$regex" : "^A" } } )
#Query for a document where name contains a letter ‘J’ and
age exists
db.people.find(
{ "name" : { $regex : "J" } , "age" : { $exists :
true } } );
$or
#
Query to find all documents in people where name ends with an ‘e’ or age exists
db.people.find(
{ $or : [ { name : { $regex : "e$" } }, { age : { $exists : true } }
] } );
# Query to find all documents where score is greater than 90
or lesser than 50
db.scores.find( { $or : [ { score : { $gt : 90 } }, { score
: { $lt : 50 } } ] } )
$and
#Find all people whose names lexicographically are greater
than ‘A’ and the name also has a ‘n’ in it
db.people.find( { $and
:[ { name : { $gt : "A" } }, { name : { $regex : "n" } } ]
} )
#The same query can be
performed without a $and. $and is not optimizable
db.people.find( { name
: { $gt : "A", $regex : "n" } } )
Querying inside Arrays
> db.people.find()
{
"_id" : ObjectId("563ae19d4192c9f554e4293c"),
"name" : "John" }
{
"_id" : ObjectId("563b8cabba646839a580a695"),
"name" : "Jane", "age" : "30",
"favorites" : [ "red", "green", "blue"
] }
{
"_id" : ObjectId("563b8cc2ba646839a580a696"),
"name" : "Gerald", "age" : "32",
"favorites" : [ "green", "blue" ] }
> db.people.find( { favorites : "red"
} )
{
"_id" : ObjectId("563b8cabba646839a580a695"), "name"
: "Jane", "age" : "30", "favorites" : [
"red",
"green", "blue" ] }
> db.people.find( { favorites : "blue" } )
{
"_id" : ObjectId("563b8cabba646839a580a695"),
"name" : "Jane", "age" : "30",
"favorites" : [ "red", "green", "blue"
] }
{
"_id" : ObjectId("563b8cc2ba646839a580a696"),
"name" : "Gerald", "age" : "32",
"favorites" : [ "green", "blue" ] }
Using $all for searching in Array
# Matches any document that has all of the specified
elements in the field irrespective of the order
>
db.people.find( { favorites : { $all : ["blue", "green"] } } )
{
"_id" : ObjectId("563b8cabba646839a580a695"),
"name" : "Jane", "age" : "30",
"favorites" : [ "red", "green", "blue" ] }
{
"_id" : ObjectId("563b8cc2ba646839a580a696"),
"name" : "Gerald", "age" : "32",
"favorites" : [ "green", "blue" ] }
Querying for nth element in an Array
Using $in for searching in Array
# Find all documents that have either of the values of name
>
db.people.find( { name : { $in : [ "Andrew", "Andy" ] } } )
{
"_id" : ObjectId("563ae1894192c9f554e42939"),
"name" : "Andrew" }
{
"_id" : ObjectId("563ae1944192c9f554e4293a"),
"name" : "Andy" }
# Find all documents that have either red or blue as
favorites
>
db.people.find( { favorites : { $in : [ "red", "blue" ] } }
)
{
"_id" : ObjectId("563b8cabba646839a580a695"), "name"
: "Jane", "age" : "30", "favorites" : [
"red", "green", "blue" ] }
{
"_id" : ObjectId("563b8cc2ba646839a580a696"),
"name" : "Gerald", "age" : "32",
"favorites" : [ "green", "blue" ] }
{
"_id" : ObjectId("563b8dfdba646839a580a697"),
"name" : "Geraldine", "age" : "32",
"favorites" : "blue" }
Nested documents
Queries with dot notation
#Insert a nested document
>
db.people.insert( { name : "Joe Biden", email : { work :
"joe@whitehouse.gov", personal : "joe@joebiden.com" } } )
WriteResult({
"nInserted" : 1 })
#The query returns a result because the email document is a
byte by byte match
>
db.people.find( { email : { work : "joe@whitehouse.gov", personal :
"joe@joebiden.com" } } )
{
"_id" : ObjectId("563b9070ba646839a580a698"),
"name" : "Joe Biden", "email" : {
"work" : "joe@whitehouse.gov", "personal" :
"joe@joebiden.com" } }
#The query does not return anything because the order of
elements in the email document is different and hence the byte by byte
comparison does not find the document.
>
db.people.find( { email : { personal : "joe@joebiden.com" }, work :
"joe@whitehouse.gov" } )
#The query does not return anything because there is only
one element in the search and the document actually has two elements.
>
db.people.find( { email : { personal : "joe@joebiden.com" } } )
#The query returns a value because of the dot notation.
>
db.people.find( { "email.personal" : "joe@joebiden.com" } )
{
"_id" : ObjectId("563b9070ba646839a580a698"),
"name" : "Joe Biden", "email" : {
"work" : "joe@whitehouse.gov", "personal" :
"joe@joebiden.com" } }
#Quotations are necessary for dot notation to be
syntactically correct.
>
db.people.find( { email.personal : "joe@joebiden.com" } )
2015-11-05T17:25:17.789+0000
E QUERY SyntaxError: Unexpected token
.
Cursors
By default .find() method is actually creating a cursor
object.
#Hold on to a cursor without printing out anything by using
a null. This ensures that the cursor is held by cur and nothing gets printed.
>
cur = db.people.find(); null;
null
hasNext()
>
cur.hasNext()
True
next()
>
cur.next()
{
"_id" :
ObjectId("563a40060e07998d8aaf2d28"),
"name" : "Smith",
"age" : 30,
"profession" :
"developer"
}
>
cur.next()
{
"_id" :
ObjectId("563a40bf0e07998d8aaf2d29"),
"name" : "Jones",
"age" : 35
}
Iterate through the cursor
>
while (cur.hasNext()) printjson(cur.next());
{
"_id" : ObjectId("563ae1894192c9f554e42939"),
"name" : "Andrew" }
{
"_id" : ObjectId("563ae1944192c9f554e4293a"),
"name" : "Andy" }
{
"_id" : ObjectId("563ae1984192c9f554e4293b"),
"name" : "Jeff" }
{
"_id" : ObjectId("563ae19d4192c9f554e4293c"),
"name" : "John" }
{
"_id" :
ObjectId("563b8cabba646839a580a695"),
"name" : "Jane",
"age" : "30",
"favorites" : [
"red",
"green",
"blue"
]
}
{
"_id" :
ObjectId("563b8cc2ba646839a580a696"),
"name" : "Gerald",
"age" : "32",
"favorites" : [
"green",
"blue"
]
}
{
"_id" :
ObjectId("563b8dfdba646839a580a697"),
"name" :
"Geraldine",
"age" : "32",
"favorites" :
"blue"
}
{
"_id" :
ObjectId("563b9070ba646839a580a698"),
"name" : "Joe Biden",
"email" : {
"work" :
"joe@whitehouse.gov",
"personal" :
"joe@joebiden.com"
}
}
limit()
#Limits the number of documents in the cursor
>
cur = db.people.find(); null;
null
>
cur.limit(3)
{
"_id" : ObjectId("563a40060e07998d8aaf2d28"),
"name" : "Smith", "age" : 30,
"profession" : "developer" }
{
"_id" : ObjectId("563a40bf0e07998d8aaf2d29"),
"name" : "Jones", "age" : 35 }
{
"_id" : ObjectId("563ae1894192c9f554e42939"),
"name" : "Andrew" }
sort()
>
cur = db.people.find(); null;
Null
#Sort
the documents in the cursor in reverse order lexicographically by name
>
cur.sort( { name : -1 } ); null;
Null
#Print
the documents in the cursor iteratively
>
while (cur.hasNext()) printjson(cur.next());
{
"_id" :
ObjectId("563a40060e07998d8aaf2d28"),
"name" : "Smith",
"age" : 30,
"profession" :
"developer"
}
{
"_id" :
ObjectId("563a40bf0e07998d8aaf2d29"),
"name" : "Jones",
"age" : 35
}
{
"_id" : ObjectId("563ae19d4192c9f554e4293c"),
"name" : "John" }
{
"_id" :
ObjectId("563b9070ba646839a580a698"),
"name" : "Joe
Biden",
"email" : {
"work" :
"joe@whitehouse.gov",
"personal" :
"joe@joebiden.com"
}
}
{
"_id" : ObjectId("563ae1984192c9f554e4293b"),
"name" : "Jeff" }
{
"_id" :
ObjectId("563b8cabba646839a580a695"),
"name" : "Jane",
"age" : "30",
"favorites" : [
"red",
"green",
"blue"
]
}
{
"_id" :
ObjectId("563b8dfdba646839a580a697"),
"name" :
"Geraldine",
"age" : "32",
"favorites" :
"blue"
}
{
"_id" :
ObjectId("563b8cc2ba646839a580a696"),
"name" : "Gerald",
"age" : "32",
"favorites" : [
"green",
"blue"
]
}
{
"_id" : ObjectId("563ae1944192c9f554e4293a"),
"name" : "Andy" }
{
"_id" : ObjectId("563ae1894192c9f554e42939"),
"name" : "Andrew" }
Chaining sort and limit
>
cur = db.people.find(); null;
Null
#sort
and limit chained
>
cur.sort( { name: -1 } ).limit(2); null;
Null
#Print
the contents of the cursor iteratively
>
while (cur.hasNext()) printjson(cur.next());
{
"_id" :
ObjectId("563a40060e07998d8aaf2d28"),
"name" : "Smith",
"age" : 30,
"profession" :
"developer"
}
{
"_id" :
ObjectId("563a40bf0e07998d8aaf2d29"),
"name" : "Jones",
"age" : 35
}
Skip
> cur = db.people.find(); null;
Null
#Skip
chained to the sort and limit
>
cur.sort( { name: -1 } ).limit(5).skip(2); null;
Null
#Print
the cursor content iteratively
>
while (cur.hasNext()) printjson(cur.next());
{
"_id" : ObjectId("563ae19d4192c9f554e4293c"),
"name" : "John" }
{
"_id" : ObjectId("563b9070ba646839a580a698"),
"name" : "Joe
Biden",
"email" : {
"work" :
"joe@whitehouse.gov",
"personal" :
"joe@joebiden.com"
}
}
{
"_id" : ObjectId("563ae1984192c9f554e4293b"),
"name" : "Jeff" }
{
"_id" :
ObjectId("563b8cabba646839a580a695"),
"name" : "Jane",
"age" : "30",
"favorites" : [
"red",
"green",
"blue"
]
}
{
"_id" :
ObjectId("563b8dfdba646839a580a697"),
"name" :
"Geraldine",
"age" : "32",
"favorites" :
"blue"
}
Counting results
Count command
>
db.people.find( { name : {$gt : "A"} } )
{
"_id" : ObjectId("563a40060e07998d8aaf2d28"),
"name" : "Smith", "age" : 30, "profession"
: "developer" }
{
"_id" : ObjectId("563a40bf0e07998d8aaf2d29"),
"name" : "Jones", "age" : 35 }
{
"_id" : ObjectId("563ae1894192c9f554e42939"),
"name" : "Andrew" }
{
"_id" : ObjectId("563ae1944192c9f554e4293a"),
"name" : "Andy" }
{
"_id" : ObjectId("563ae1984192c9f554e4293b"),
"name" : "Jeff" }
{
"_id" : ObjectId("563ae19d4192c9f554e4293c"),
"name" : "John" }
{
"_id" : ObjectId("563b8cabba646839a580a695"),
"name" : "Jane", "age" : "30",
"favorites" : [ "red", "green", "blue"
] }
{
"_id" : ObjectId("563b8cc2ba646839a580a696"),
"name" : "Gerald", "age" : "32",
"favorites" : [ "green", "blue" ] }
{
"_id" : ObjectId("563b8dfdba646839a580a697"),
"name" : "Geraldine", "age" : "32",
"favorites" : "blue" }
{
"_id" : ObjectId("563b9070ba646839a580a698"), "name"
: "Joe Biden", "email" : { "work" :
"joe@whitehouse.gov", "personal" :
"joe@joebiden.com" } }
>
db.people.count( { name : {$gt : "A"} } )
10
$set and $inc command
$set command
The command is used for setting specific values in a
document using the update command. This command retains the data for all other
fields in the document unlike the update command when used without the $set.
#
Find a document where name is Jane
>
db.people.find( { name : "Jane" } ).pretty()
{
"_id" : ObjectId("563b8cabba646839a580a695"),
"name" : "Jane",
"age" : "30",
"favorites" : [
"red",
"green",
"blue"
]
}
# Set
the age for Jane to 35
>
db.people.update( { name : "Jane"}, { $set : { age : 35 }} )
WriteResult({
"nMatched" : 1, "nUpserted" : 0, "nModified" : 1
})
>
db.people.find( { name : "Jane" } ).pretty()
{
"_id" :
ObjectId("563b8cabba646839a580a695"),
"name" : "Jane",
"age" : 35,
"favorites" : [
"red",
"green",
"blue"
]
}
$inc command
The command is used for
incrementing a value in a document using the update command.
#
Increment the age of Jane by one using $inc
>
db.people.update( { name : "Jane"}, { $inc : { age : 1 }} )
WriteResult({
"nMatched" : 1, "nUpserted" : 0, "nModified" : 1
})
>
db.people.find( { name : "Jane" } ).pretty()
{
"_id" :
ObjectId("563b8cabba646839a580a695"),
"name" : "Jane",
"age" : 36,
"favorites" : [
"red",
"green",
"blue"
]
}
$unset command
Removes the field and the value from a specified document
that matches the search criteria.
>
db.people.find( { name : "Jane" } ).pretty()
{
"_id" :
ObjectId("563b8cabba646839a580a695"),
"name" : "Jane",
"age" : 36,
"favorites" : [
"red",
"green",
"blue"
]
}
>
db.people.update( { name : "Jane" }, { $unset : { favorites : 1 } } )
WriteResult({
"nMatched" : 1, "nUpserted" : 0, "nModified" : 1
})
>
db.people.find( { name : "Jane" } ).pretty()
{
"_id" :
ObjectId("563b8cabba646839a580a695"),
"name" : "Jane",
"age" : 36
}
Array operations
>
db.arrays.insert( { _id : 0 , a : [ 1, 2, 3, 4 ]} )
WriteResult({
"nInserted" : 1 })
>
db.arrays.update( { _id : 0 }, { $set
: { "a.2" : 5 } } )
WriteResult({
"nMatched" : 1, "nUpserted" : 0, "nModified" : 1
})
>
db.arrays.find()
{
"_id" : 0, "a" : [ 1, 2, 5, 4 ] }
>
db.arrays.update( { _id : 0 }, { $push :
{ a : 6 } } )
WriteResult({
"nMatched" : 1, "nUpserted" : 0, "nModified" : 1
})
>
db.arrays.find()
{
"_id" : 0, "a" : [ 1, 2, 5, 4, 6 ] }
>
db.arrays.update( { _id : 0 }, { $pop
: { a : 1 } } )
WriteResult({
"nMatched" : 1, "nUpserted" : 0, "nModified" : 1
})
>
db.arrays.update( { _id : 0 }, { $pop : { a : -1 } } )
WriteResult({
"nMatched" : 1, "nUpserted" : 0, "nModified" : 1
})
>
db.arrays.find()
{
"_id" : 0, "a" : [ 2, 5, 4 ] }
>
db.arrays.update( { _id : 0 }, { $pushAll
: { a : [ 7, 8, 9, 10 ] } } )
WriteResult({
"nMatched" : 1, "nUpserted" : 0, "nModified" : 1
})
>
db.arrays.find()
{
"_id" : 0, "a" : [ 2, 5, 4, 7, 8, 9, 10 ] }
>
db.arrays.update( { _id : 0 }, { $pull
: { a : 5 } } )
WriteResult({
"nMatched" : 1, "nUpserted" : 0, "nModified" : 1
})
>
db.arrays.find()
{
"_id" : 0, "a" : [ 2, 4, 7, 8, 9, 10 ] }
>
db.arrays.update( { _id : 0 }, { $pullAll
: { a : [ 2, 7, 8 ] } } )
WriteResult({
"nMatched" : 1, "nUpserted" : 0, "nModified" : 1
})
>
db.arrays.find()
{
"_id" : 0, "a" : [ 4, 9, 10 ] }
>
db.arrays.update( { _id : 0 }, { $addToSet
: { a : 5 } } )
WriteResult({
"nMatched" : 1, "nUpserted" : 0, "nModified" : 1
})
>
db.arrays.update( { _id : 0 }, { $addToSet : { a : 5 } } )
WriteResult({
"nMatched" : 1, "nUpserted" : 0, "nModified" : 0
})
>
db.arrays.find()
{
"_id" : 0, "a" : [ 4, 9, 10, 5 ] }
Upsert command
Insert if the document searched for updating does not exist.
>
db.people.find()
{
"_id" : ObjectId("563a40060e07998d8aaf2d28"),
"name" : "Smith", "age" : 30,
"profession" : "developer" }
{
"_id" : ObjectId("563a40bf0e07998d8aaf2d29"),
"name" : "Jones", "age" : 35 }
{
"_id" : ObjectId("563ae1894192c9f554e42939"), "name"
: "Andrew" }
{
"_id" : ObjectId("563ae1944192c9f554e4293a"),
"name" : "Andy" }
{
"_id" : ObjectId("563ae1984192c9f554e4293b"),
"name" : "Jeff" }
{
"_id" : ObjectId("563ae19d4192c9f554e4293c"),
"name" : "John" }
{
"_id" : ObjectId("563b8cabba646839a580a695"),
"name" : "Jane", "age" : 36 }
{
"_id" : ObjectId("563b8cc2ba646839a580a696"),
"name" : "Gerald", "age" : "32",
"favorites" : [ "green", "blue" ] }
{
"_id" : ObjectId("563b8dfdba646839a580a697"),
"name" : "Geraldine", "age" : "32",
"favorites" : "blue" }
{
"_id" : ObjectId("563b9070ba646839a580a698"),
"name" : "Joe Biden", "email" : {
"work" : "joe@whitehouse.gov", "personal" :
"joe@joebiden.com" } }
#Update on a document that does not exist does nothing.
>
db.people.update( { name : "Charlie" }, { $set : { age : 40 } } )
WriteResult({
"nMatched" : 0, "nUpserted" : 0, "nModified" : 0
})
#Update with a upsert on a document that does not exist,
creates the document.
>
db.people.update( { name : "Charlie" }, { $set : { age : 40 } }, {
upsert : true } )
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" :
ObjectId("564420e692781bd83e60cd23")
})
>
db.people.find( { name : "Charlie" })
{
"_id" : ObjectId("564420e692781bd83e60cd23"),
"name" : "Charlie", "age" : 40 }
Multiupdate
#multi : true is required for updating all documents in a
collection.
# {} in the search means ‘all documents in the collection’
>
db.people.update( {}, { $set : { title : "Dr" } }, { multi : true } )
WriteResult({
"nMatched" : 11, "nUpserted" : 0, "nModified" :
11 })
>
db.people.find()
{
"_id" : ObjectId("563a40060e07998d8aaf2d28"),
"name" : "Smith", "age" : 30,
"profession" : "developer", "title" :
"Dr" }
{
"_id" : ObjectId("563a40bf0e07998d8aaf2d29"),
"name" : "Jones", "age" : 35, "title" :
"Dr" }
{
"_id" : ObjectId("563b8cabba646839a580a695"),
"name" : "Jane", "age" : 36, "title" :
"Dr" }
{
"_id" : ObjectId("563b8cc2ba646839a580a696"),
"name" : "Gerald", "age" : "32",
"favorites" : [ "green", "blue" ],
"title" : "Dr" }
{
"_id" : ObjectId("563b8dfdba646839a580a697"),
"name" : "Geraldine", "age" : "32",
"favorites" : "blue", "title" : "Dr" }
{
"_id" : ObjectId("563b9070ba646839a580a698"),
"name" : "Joe Biden", "email" : {
"work" : "joe@whitehouse.gov", "personal" :
"joe@joebiden.com" }, "title" : "Dr" }
{
"_id" : ObjectId("564420e692781bd83e60cd23"),
"name" : "Charlie", "age" : 40, "title"
: "Dr" }
{
"_id" : ObjectId("563ae1894192c9f554e42939"),
"name" : "Andrew", "title" : "Dr" }
{
"_id" : ObjectId("563ae1944192c9f554e4293a"), "name"
: "Andy", "title" : "Dr" }
{
"_id" : ObjectId("563ae1984192c9f554e4293b"),
"name" : "Jeff", "title" : "Dr" }
{
"_id" : ObjectId("563ae19d4192c9f554e4293c"),
"name" : "John", "title" : "Dr" }
>
Remove
#Remove a specific document
db.people.remove( { name : “John” } )
db.people.remove( { name : “John” } )
#Remove documents one by one
db.people.remove( {} )
db.people.remove( {} )
#Remove all documents in the collection
db.people.drop()
db.people.drop()
Distinct values
db.inventory.distinct( "dept" )Create a user with admin privileges to all databases
db.createUser({user:"replSetManager",pwd:"password",roles:[{role:"clusterManager",db:"admin"},{role:"dbOwner", db:"adminsblog"},{role:"readWrite", db:"departmentblog"},{role:"read", db:"otherblog"}]})