1.打开mongodb shell窗口
[root@iZ94b9jaqbfZ bin]# cd /usr/mongodb-linux-x86_64-3.4.3/bin
[root@iZ94b9jaqbfZ bin]# ls
bsondump mongod mongodump mongofiles mongooplog mongoreplay mongos mongotop
mongo mongodb.conf mongoexport mongoimport mongoperf mongorestore mongostat
[root@iZ94b9jaqbfZ bin]# ./mongo
MongoDB shell version v3.4.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.3
Server has startup warnings:
2017-04-03T20:21:01.683+0800 I STORAGE [initandlisten]
2017-04-03T20:21:01.683+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-04-03T20:21:01.683+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-04-03T20:21:03.058+0800 I CONTROL [initandlisten]
2017-04-03T20:21:03.059+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-04-03T20:21:03.059+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-04-03T20:21:03.059+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-04-03T20:21:03.059+0800 I CONTROL [initandlisten]
2017-04-03T20:21:03.062+0800 I CONTROL [initandlisten]
2017-04-03T20:21:03.062+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 1024 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
>
2.创建使用数据库
use DBNAME 当库不存在时会创建
> use test
switched to db test
> show dbs
admin 0.000GB
local 0.000GB
show dbs查看所有数据库,但是新创建的数据库不会被列出,当其中至少有一条数据时才回被列出。
> db.abc.insert({"key":"test","val":"这是一条测试数据"})
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.000GB
local 0.000GB
test 0.000GB
插入数据后正常显示
3.删除数据库
db.dropDatabase()删除数据库
> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }
db.collection.drop() 删除某一集合数据
> db.abc.drop()
true
3.向集合中插入数据(对象,不能插入字符串、数字等)
> db.abc.insert({"key":"test","val":"这是一条测试数据"})
WriteResult({ "nInserted" : 1 })
> db.abc.insert({"key":"test","val":"这是一条测试数据"})
WriteResult({ "nInserted" : 1 })
> db.abc.find()
{ "_id" : ObjectId("58e24b2ad14f02725996ff07"), "key" : "test", "val" : "这是一条测试数据" }
{ "_id" : ObjectId("58e24bc2d14f02725996ff08"), "key" : "test", "val" : "这是一条测试数据" }
4.查询、更新、删除
> db.abc.find()
{ "_id" : ObjectId("58e24b2ad14f02725996ff07"), "key" : "test", "val" : "这是一条测试数据" }
{ "_id" : ObjectId("58e24bc2d14f02725996ff08"), "key" : "test", "val" : "这是一条测试数据" }
> db.abc.find({ "_id" : ObjectId("58e24b2ad14f02725996ff07")})
{ "_id" : ObjectId("58e24b2ad14f02725996ff07"), "key" : "test", "val" : "这是一条测试数据" }
> db.abc.update({ "_id" : ObjectId("58e24b2ad14f02725996ff07")},{$set:{'val':"aaa"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.abc.find({ "_id" : ObjectId("58e24b2ad14f02725996ff07")})
{ "_id" : ObjectId("58e24b2ad14f02725996ff07"), "key" : "test", "val" : "aaa" }
> db.abc.remove({ "_id" : ObjectId("58e24b2ad14f02725996ff07")})
WriteResult({ "nRemoved" : 1 })
> db.abc.find({ "_id" : ObjectId("58e24b2ad14f02725996ff07")})
> db.abc.find()
{ "_id" : ObjectId("58e24bc2d14f02725996ff08"), "key" : "test", "val" : "这是一条测试数据" }