手册:https://docs.mongodb.com/manual/reference/method/db.collection.find/
1.远程连接
docker run -it mongo bash
要连接的IP:116.85.12.51
命令:mongo 116.85.12.51:27017/meeting -u "kimkat" -p "123456"
这样就可以连接到116.85.12.51的mongodb/meeting的数据库了。
2.创建用户授权
use admin;
show users;
mgset-410052412:PRIMARY>
db.createUser(
{
user:"bch",
pwd:"kmLnBNyycD7gtBom",
roles:[{"role" : "readWrite","db" : "bitcoin_cash"}]
}
)
3.删除数据库
> show dbs
> use runoob
switched to db runoob
> db.dropDatabase()
{ "dropped" : "runoob", "ok" : 1 }
4.删除集合
集合删除语法格式如下:
db.collection.drop()
以下实例删除了 runoob 数据库中的集合 site:
> use runoob
switched to db runoob
> db.createCollection("runoob") # 先创建集合,类似数据库中的表
> show tables
runoob
> db.runoob.drop()
true
> show tables
>
5. 根据条件查询表/集合数据
> show collections
accounts
> db.collectionName.find() 或者 db.collectionName.find({})
- > db.collection.find(query,projection)
参数 | 类型 | 描述 |
query | document | 可选. 使用查询操作符指定查询条件 |
projection | document | 指定使用投影运算符返回的字段省略此参数返回匹配文档中的所有字段 |
https://www.cnblogs.com/williamjie/p/9943739.html
6. 其它语法
善用help
mgset-400047458:PRIMARY> 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
mgset-400047458:PRIMARY>
mgset-400047458:PRIMARY> 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) - will only function with MongoDB 4.0 and below
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost) - will only function with MongoDB 4.0 and below
db.createCollection(name, {size: ..., capped: ..., max: ...})
db.createUser(userDocument)
db.createView(name, viewOn, [{$operator: {...}}, ...], {viewOptions})
db.currentOp() displays currently executing operations in the db
db.dropDatabase(writeConcern)
db.dropUser(username)
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.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.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.setVerboseShell(flag) display extra information in shell output
db.setWriteConcern(<write concern doc>) - sets the write concern for writes to the db
db.shutdownServer()
db.stats()
db.unsetWriteConcern(<write concern doc>) - unsets the write concern for writes to the db
db.version() current version of the server
db.watch() - opens a change stream cursor for a database to report on all changes to its non-system collections.
mgset-400047458:PRIMARY>