运行
$ mongo
MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1f3d0d3a-5bb2-4e93-8dc5-5dcd037d651a") }
MongoDB server version: 4.0.10
>
help
MongoDB Shell 内置了帮助文档。
> 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
>
db.help()是数据库级别的帮助文档:
> 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) - deprecated
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost) - deprecated
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.{collection}.help()查看集合级别的帮助文档:
> use machine
switched to db machine
> db.machine.help()
DBCollection help
db.machine.find().help() - show DBCursor help
db.machine.bulkWrite( operations, <optional params> ) - bulk execute write operations, optional parameters are: w, wtimeout, j
db.machine.count( query = {}, <optional params> ) - count the number of documents that matches the query, optional parameters are: limit, skip, hint, maxTimeMS
db.machine.countDocuments( query = {}, <optional params> ) - count the number of documents that matches the query, optional parameters are: limit, skip, hint, maxTimeMS
db.machine.estimatedDocumentCount( <optional params> ) - estimate the document count using collection metadata, optional parameters are: maxTimeMS
db.machine.copyTo(newColl) - duplicates collection by copying all documents to newColl; no indexes are copied.
db.machine.convertToCapped(maxBytes) - calls {convertToCapped:'machine', size:maxBytes}} command
db.machine.createIndex(keypattern[,options])
db.machine.createIndexes([keypatterns], <options>)
db.machine.dataSize()
db.machine.deleteOne( filter, <optional params> ) - delete first matching document, optional parameters are: w, wtimeout, j
db.machine.deleteMany( filter, <optional params> ) - delete all matching documents, optional parameters are: w, wtimeout, j
db.machine.distinct( key, query, <optional params> ) - e.g. db.machine.distinct( 'x' ), optional parameters are: maxTimeMS
db.machine.drop() drop the collection
db.machine.dropIndex(index) - e.g. db.machine.dropIndex( "indexName" ) or db.machine.dropIndex( { "indexKey" : 1 } )
db.machine.dropIndexes()
db.machine.ensureIndex(keypattern[,options]) - DEPRECATED, use createIndex() instead
db.machine.explain().help() - show explain help
db.machine.reIndex()
db.machine.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.
e.g. db.machine.find( {x:77} , {name:1, x:1} )
db.machine.find(...).count()
db.machine.find(...).limit(n)
db.machine.find(...).skip(n)
db.machine.find(...).sort(...)
...
>
查看函数信息,直接输入函数名就可以了:
> db.machine.find
function (query, fields, limit, skip, batchSize, options) {
var cursor = new DBQuery(this._mongo,
this._db,
this,
this._fullName,
this._massageObject(query),
fields,
limit,
skip,
batchSize,
options || this.getQueryOptions());
{
const session = this.getDB().getSession();
const readPreference = session._serverSession.client.getReadPreference(session);
if (readPreference !== null) {
cursor.readPref(readPreference.mode, readPreference.tags);
}
const readConcern = session._serverSession.client.getReadConcern(session);
if (readConcern !== null) {
cursor.readConcern(readConcern.level);
}
}
return cursor;
}
>
网友评论