Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
OPEN
H ere are the most commonly used Mongo Shell commands with
example usage.
Basic Commands
Run this
To do this Example
command
Connect to local
host on default mongo mongo
port 27017
mongo --host
Connect to remote mongo --host
<hostname or ip
host on speci ed 10.121.65.23 --port
address> --port
port 23020
<port no>
Show current
db db
database
Display help on DB
db.help() db.help()
methods
Display help on
db.mycol.help() db.mycol.help()
Collection
Show Commands
Show all databases show dbs show dbs
CRUD Operations
db.books.insert({"is
bn":
9780060859749,
Insert a new "title": "After Alice:
db.collection.insert(
document in a A Novel", "author":
<document> )
collection [2] "Gregory Maguire",
"category":
"Fiction",
"year":2016})
Show all
documents in the db.collection. nd() db.books. nd()
collection
db.books. nd({"title
Show only some db.collection. nd(< ":"Treasure
elds of matching query>, Island"}, {title:true,
documents <projection>) category:true,
_id:false})
db.books.update({ti
Update speci c
tle : "Treasure
elds of a single db.collection.updat
Island"}, {$set :
document that e(<query>,
{category
match the query <update> )
:"Adventure
condition
Fiction"}})
Delete all
db.books.remove({"
documents db.collection.remo
category"
matching a query ve(<query>)
:"Fiction"})
condition
Delete all
db.collection.remo db.books.remove({}
documents in a
ve({}) )
collection
Index
Create an index db.collection.create db.books.createInd
Index( ex({title:1})
{indexField:type} )
Type 1 means
ascending; -1
means descending
db.collection.create
db.books.createInd
Create a unique Index(
ex( {isbn:1},
index {indexField:type},
{unique:true} )
{unique:true} )
db.collection.create
Create a index on Index({indexField1: db.books.createInd
multiple elds type1, ex({title:1,
(compound index) indexField2:type2, author:-1})
...})
db.collection.dropI
db.books.dropInde
Drop an index ndex(
x({author:-1})
{indexField:type} )
Show index
db.collection.stats() db.books.stats()
statistics
Cursor Methods
Show number of
db.books. nd().cou
documents in the cursor.count()
nt()
collection
Display formatted
db.books. nd({}).pr
(more readable) cursor.pretty()
etty()
result
Comparison Operators
{< eld>: { $eq: db.books. nd({year
equals to
<value> }} : {$eq: 2016}})
Logical Operators
db.books. nd( {
{ $or:
$or: [{year: {$lte:
OR [<expression1>,
2008}}, {year: {$eq:
<expression2>,...]}
2016}}]} )
db.books. nd(
{ $not:
NOT {$not: {year: {$eq:
{<expression>}}
2016} }})
db.books. nd( {
{ $nor:
$nor: [{year: {$lte:
NOR [<expression1>,
2008}}, {year: {$eq:
<expression2>,...]}
2016}}]} )
Element Operators
Match documents db.books. nd(
{< eld>:
that contains that {category:
{$exists:true}}
speci ed eld {$exists: true }})
Match documents
db.books. nd(
whose eld value is {< eld>:
{category: {$type:
of the speci ed {$type:value}}
2 }})
BSON data type
[1] Databases are created on the y and will actually be created when
you insert something into it.
[2] Collections are created on the y when you insert rst document
into it.
Rate it
Related Articles
Post a comment
Comments
Your Comment....
Name Email
Post Comment