root@adela-vm:/opt/mongodb_simple# numactl --interleave=all bin/mongod -f conf/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 4904
child process started successfully, parent exiting
root@adela-vm:/opt/mongodb_simple# ./bin/mongo 127.0.0.1:12345
MongoDB shell version: 2.6.9
connecting to: 127.0.0.1:12345/test
> show dbs //显示有几个数据库
admin (empty)
local 0.078GB
> use adela //切换到adela数据库,不需要事先定义
switched to db adela
> show dbs //adela数据库没有增加内容,所以不现实
admin (empty)
local 0.078GB
>
RDBMS中的表 对应 mongoDB中的集合collections ,插入内容为json格式
db.集合名(表).insert(json格式)
db.adela_collection.insert({"x":1})
显示所有的集合
> show collections
adela_collection
system.indexes
插入数据
> db.adela_collection.insert({y:2})
WriteResult({ "nInserted" : 1 })
> db.adela_collection.find()
{ "_id" : ObjectId("5514f09641daf13c28f7bdb7"), "x" : 1 }
{ "_id" : ObjectId("5514f12b41daf13c28f7bdb8"), "y" : 2 }
_id是系统自动分配的,也可以自定义,但是 _id 不能重复
> db.adela_collection.insert({a:1,_id:1})
WriteResult({ "nInserted" : 1 })
> db.adela_collection.insert({a:2,_id:1})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: adela.adela_collection.$_id_ dup key: { : 1.0 }"
}
})
查询adela_collection全表
> db.adela_collection.find() //不加参数
查询x为1的数据
> db.adela_collection.find({x:1})
{ "_id" : ObjectId("5514f09641daf13c28f7bdb7"), "x" : 1 }
{ "_id" : ObjectId("5514f1d241daf13c28f7bdb9"), "x" : 1 }
循环插入一批数据
> for(i=1;i<11;i++)db.adela_collection.insert({z:i})
WriteResult({ "nInserted" : 1 })
> db.adela_collection.find()
{ "_id" : ObjectId("5514f09641daf13c28f7bdb7"), "x" : 1 }
{ "_id" : ObjectId("5514f12b41daf13c28f7bdb8"), "y" : 2 }
{ "_id" : ObjectId("5514f1d241daf13c28f7bdb9"), "x" : 1 }
{ "_id" : 1, "a" : 1 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe54"), "z" : 1 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe55"), "z" : 2 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe56"), "z" : 3 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe57"), "z" : 4 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe58"), "z" : 5 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe59"), "z" : 6 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5a"), "z" : 7 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5b"), "z" : 8 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5c"), "z" : 9 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5d"), "z" : 10 }
计数count
> db.adela_collection.find().count()
14
skip跳过多少条数据
limit限制返回多少条数据
sort使用排序
> db.adela_collection.find()
{ "_id" : ObjectId("5514f09641daf13c28f7bdb7"), "x" : 1 }
{ "_id" : ObjectId("5514f12b41daf13c28f7bdb8"), "y" : 2 }
{ "_id" : ObjectId("5514f1d241daf13c28f7bdb9"), "x" : 1 }
{ "_id" : 1, "a" : 1 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe54"), "z" : 1 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe55"), "z" : 2 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe56"), "z" : 3 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe57"), "z" : 4 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe58"), "z" : 5 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe59"), "z" : 6 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5a"), "z" : 7 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5b"), "z" : 8 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5c"), "z" : 9 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5d"), "z" : 10 }
> db.adela_collection.find().skip(4)
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe54"), "z" : 1 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe55"), "z" : 2 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe56"), "z" : 3 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe57"), "z" : 4 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe58"), "z" : 5 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe59"), "z" : 6 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5a"), "z" : 7 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5b"), "z" : 8 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5c"), "z" : 9 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe5d"), "z" : 10 }
> db.adela_collection.find().skip(4).limit(5)
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe54"), "z" : 1 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe55"), "z" : 2 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe56"), "z" : 3 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe57"), "z" : 4 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe58"), "z" : 5 }
> db.adela_collection.find().skip(4).limit(5).sort({z:1})
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe54"), "z" : 1 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe55"), "z" : 2 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe56"), "z" : 3 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe57"), "z" : 4 }
{ "_id" : ObjectId("5514f5eca1b17c0bb936fe58"), "z" : 5 }