-
Notifications
You must be signed in to change notification settings - Fork 3
/
01-shell-important-commands.txt
148 lines (139 loc) · 8.67 KB
/
01-shell-important-commands.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
mongod --help
> help
db.help() help on db methods
db.mycoll.help() help on collection methods
sh.help() sharding helpers
rs.help() replica set helpers
help admin administrative help
help connect connecting to a db help
help keys key shortcuts
help misc misc things to know
help mr mapreduce
show dbs show database names
show collections show collections in current database
show users show users in current database
show profile show most recent system.profile entries with time >= 1ms
show logs show the accessible logger names
show log [name] prints out the last segment of log in memory, 'global' is default
use <db_name> set current database
db.foo.find() list objects in collection foo
db.foo.find( { a : 1 } ) list objects in foo where a == 1
it result of the last line evaluated; use to further iterate
DBQuery.shellBatchSize = x set default number of items to display on shell
exit quit the mongo shell
> use shop
switched to db shop
> db.help()
DB methods:
db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [just calls db.runCommand(...)]
db.aggregate([pipeline], {options}) - performs a collectionless aggregation on this database; returns a cursor
db.auth(username, password)
db.cloneDatabase(fromhost)
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost)
db.createCollection(name, {size: ..., capped: ..., max: ...})
db.createView(name, viewOn, [{$operator: {...}}, ...], {viewOptions})
db.createUser(userDocument)
db.currentOp() displays currently executing operations in the db
db.dropDatabase()
db.eval() - deprecated
db.fsyncLock() flush data to disk and lock server for backups
db.fsyncUnlock() unlocks server following a db.fsyncLock()
db.getCollection(cname) same as db['cname'] or db.cname
db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db's collections
db.getCollectionNames()
db.getLastError() - just returns the err msg string
db.getLastErrorObj() - return full status object
db.getLogComponents()
db.getMongo() get the server connection object
db.getMongo().setSlaveOk() allow queries on a replication slave server
db.getName()
db.getPrevError()
db.getProfilingLevel() - deprecated
db.getProfilingStatus() - returns if profiling is on and slow threshold
db.getReplicationInfo()
db.getSiblingDB(name) get the db at the same server as this one
db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set
db.hostInfo() get details about the server's host
db.isMaster() check replica primary status
db.killOp(opid) kills the current operation in the db
db.listCommands() lists all the db commands
db.loadServerScripts() loads all the scripts in db.system.js
db.logout()
db.printCollectionStats()
db.printReplicationInfo()
db.printShardingStatus()
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.setLogLevel(level,<component>)
db.setProfilingLevel(level,slowms) 0=off 1=slow 2=all
db.setWriteConcern(<write concern doc>) - sets the write concern for writes to the db
db.unsetWriteConcern(<write concern doc>) - unsets the write concern for writes to the db
db.setVerboseShell(flag) display extra information in shell output
db.shutdownServer()
db.stats()
db.version() current version of the server
> show collections
products
> db.products.help()
DBCollection help
db.products.find().help() - show DBCursor help
db.products.bulkWrite( operations, <optional params> ) - bulk execute write operations, optional parameters are: w, wtimeout, j
db.products.count( query = {}, <optional params> ) - count the number of documents that matches the query, optional parameters are: limit, skip, hint, maxTimeMS
db.products.copyTo(newColl) - duplicates collection by copying all documents to newColl; no indexes are copied.
db.products.convertToCapped(maxBytes) - calls {convertToCapped:'products', size:maxBytes}} command
db.products.createIndex(keypattern[,options])
db.products.createIndexes([keypatterns], <options>)
db.products.dataSize()
db.products.deleteOne( filter, <optional params> ) - delete first matching document, optional parameters are: w, wtimeout, j
db.products.deleteMany( filter, <optional params> ) - delete all matching documents, optional parameters are: w, wtimeout, j
db.products.distinct( key, query, <optional params> ) - e.g. db.products.distinct( 'x' ), optional parameters are: maxTimeMS
db.products.drop() drop the collection
db.products.dropIndex(index) - e.g. db.products.dropIndex( "indexName" ) or db.products.dropIndex( { "indexKey" : 1 } )
db.products.dropIndexes()
db.products.ensureIndex(keypattern[,options]) - DEPRECATED, use createIndex() instead
db.products.explain().help() - show explain help
db.products.reIndex()
db.products.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.
e.g. db.products.find( {x:77} , {name:1, x:1} )
db.products.find(...).count()
db.products.find(...).limit(n)
db.products.find(...).skip(n)
db.products.find(...).sort(...)
db.products.findOne([query], [fields], [options], [readConcern])
db.products.findOneAndDelete( filter, <optional params> ) - delete first matching document, optional parameters are: projection, sort, maxTimeMS
db.products.findOneAndReplace( filter, replacement, <optional params> ) - replace first matching document, optional parameters are: projection, sort, maxTimeMS, upsert, returnNewDocument
db.products.findOneAndUpdate( filter, update, <optional params> ) - update first matching document, optional parameters are: projection, sort, maxTimeMS, upsert, returnNewDocument
db.products.getDB() get DB object associated with collection
db.products.getPlanCache() get query plan cache associated with collection
db.products.getIndexes()
db.products.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )
db.products.insert(obj)
db.products.insertOne( obj, <optional params> ) - insert a document, optional parameters are: w, wtimeout, j
db.products.insertMany( [objects], <optional params> ) - insert multiple documents, optional parameters are: w, wtimeout, j
db.products.mapReduce( mapFunction , reduceFunction , <optional params> )
db.products.aggregate( [pipeline], <optional params> ) - performs an aggregation on a collection; returns a cursor
db.products.remove(query)
db.products.replaceOne( filter, replacement, <optional params> ) - replace the first matching document, optional parameters are: upsert, w, wtimeout, j
db.products.renameCollection( newName , <dropTarget> ) renames the collection.
db.products.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name
db.products.save(obj)
db.products.stats({scale: N, indexDetails: true/false, indexDetailsKey: <index key>, indexDetailsName: <index name>})
db.products.storageSize() - includes free space allocated to this collection
db.products.totalIndexSize() - size in bytes of all the indexes
db.products.totalSize() - storage allocated for all data and indexes
db.products.update( query, object[, upsert_bool, multi_bool] ) - instead of two flags, you can pass an object with fields: upsert, multi
db.products.updateOne( filter, update, <optional params> ) - update the first matching document, optional parameters are: upsert, w, wtimeout, j
db.products.updateMany( filter, update, <optional params> ) - update all matching documents, optional parameters are: upsert, w, wtimeout, j
db.products.validate( <full> ) - SLOW
db.products.getShardVersion() - only for use with sharding
db.products.getShardDistribution() - prints statistics about data distribution in the cluster
db.products.getSplitKeysForChunks( <maxChunkSize> ) - calculates split points over all chunks and returns splitter function
db.products.getWriteConcern() - returns the write concern used for any operations on this collection, inherited from server/db if set
db.products.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the collection
db.products.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the collection
db.products.latencyStats() - display operation latency histograms for this collection