windows启动MongoDB :cmd进入到安装目录(D:\mongodb\bin)
mongod --dbpath D:\MongoDB\data\db
新开cmd 进入D:\mongodb\bin 输入mongo成功启动页面:
连接到指定机器名和端口:
mongo host:port/DB(如:mongo 192.168.1.2:27017/myDB)
或者启动mongo时候不连接数据库,需要时在运行new Mongo(hostname)就可以连接指定数据库
mongo --npdb
>conn = new Mongo("192.168.1.2:27017")
>db = conn.getDB("myDB")
基本命令:输入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
其他常用命令:
db.version()显示当前db版本
db.getName()查看当前使用的数据库
数据库命令:
插入数据: db.foo.save({name: "'swain'", age: 26, sex: true});
查找数据:db.foo.find();
删除数据:db.foo.drop();