您好,登錄后才能下訂單哦!
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 }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。