1.启动mongo服务:mongod.exe
D:\MongoDB\Server\4.0\bin>mongod
2.连接到mongo shell:mongo.exe
D:\MongoDB\Server\4.0\bin>mongo
D:\MongoDB\Server\4.0\bin>mongo
MongoDB shell version v4.0.3
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("41167926-5680-4cdb-b74c-a81942657d7c")}
MongoDB server version: 4.0.3
Server has startup warnings:
2021-05-18T08:11:04.617+0800 I CONTROL [initandlisten]
2021-05-18T08:11:04.617+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2021-05-18T08:11:04.617+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2021-05-18T08:11:04.617+0800 I CONTROL [initandlisten]
3.切换数据库:use xxx
MongoDB Enterprise > use simman
MongoDB Enterprise > use simman
switched to db simman
4.显示当前shell连接的数据库:db
MongoDB Enterprise > db
MongoDB Enterprise > db
simman
5.显示所有的数据库:show dbs
MongoDB Enterprise > show dbs
MongoDB Enterprise > show dbs
admin 0.000GB
config 0.000GB
fengwo 0.290GB
fwdata 0.083GB
local 0.000GB
longsp 0.005GB
simman 3.056GB
xhb 0.037GB
6.显示当前连接数据库的集合:show collections
MongoDB Enterprise > show collections
MongoDB Enterprise > show collections
log
manualCheckRecord
overChargeAlert
sim
simCancle
simCancleOperateOrder
simChange
7.检查集合里面的数据:db.<collectionName>.find()
MongoDB Enterprise > db.sim.find()
MongoDB Enterprise > db.sim.find()
{ "_id" : ObjectId("56c3f3b03182c61648ff65c6"), "simNum" : "1061110890000", "stockPackageName" : "56d7acbceb6aa918288e95
e6", "packageId" : "", "companyId" : "", "companyName" : "", "importBatchNumber" : NumberLong("1458033886804"), "importedByUserId" : "18611111111", "importedByU
serName" : "张sw", "importedTime" : ISODate("2016-03-15T09:24:46.849Z"), "outputBatch" : 0, "distributeBatch" : 0, "distributeByUserId" : "", "distributeByUse
rName" : "", "iccid" : "892202B1111553472222", "imsi" : "460022238302111", "status" : 2, "smsSent" : 0, "smsAlarm" : false, "trafficUsed" : 0, "trafficLeft" : 3
0720, "balance" : 0, "trafficAlarm" : false, "beginDate" : ISODate("2015-08-10T10:39:46Z"), "useDate" : ISODate("2015-09-13T16:00:00Z"), "expireDate" : ISODate(
"2018-12-02T15:59:59.999Z"), "cardinfoUpdateTime" : ISODate("2018-08-02T02:45:38.453Z"), "balanceUpdateTime" : ISODate("2019-01-27T20:53:35.641Z"), "userStatusU
8.获取关于db或collection的方法帮助:db.help()或者db.<collectionName>.help()
MongoDB Enterprise > db.help()
MongoDB Enterprise > 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.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
9.获取帮助:help
MongoDB Enterprise > help
MongoDB Enterprise > 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 mapreduceshow 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