mongodb的基礎(chǔ)就是針對文集的增刪改查,在此只列一些基礎(chǔ)的內(nèi)容
insert
- 正常插入:
> db.user.insert({x:1,y:2,z:3})
WriteResult({ "nInserted" : 1 })
- 循環(huán)插入
> for(var i=1;i<100;i++) db.user.insert({x:i, y:i*10, z:i*100})
WriteResult({ "nInserted" : 1 })
update
- 整個(gè)更新
> db.user.find()
{ "_id" : ObjectId("58d0d844bae387d0c1e8d870"), "x" : 1, "y" : 2 }
{ "_id" : ObjectId("58d0d84ebae387d0c1e8d871"), "x" : 2, "y" : 3 }
> db.user.update({x:1},{x:999})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : ObjectId("58d0d844bae387d0c1e8d870"), "x" : 999 }
{ "_id" : ObjectId("58d0d84ebae387d0c1e8d871"), "x" : 2, "y" : 3 }
- 部分更新
使用$set操作符
> db.user.find()
{ "_id" : ObjectId("58d0d844bae387d0c1e8d870"), "x" : 999 }
{ "_id" : ObjectId("58d0d84ebae387d0c1e8d871"), "x" : 2, "y" : 3 }
> db.user.update({x:2},{$set:{x:999}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : ObjectId("58d0d844bae387d0c1e8d870"), "x" : 999 }
{ "_id" : ObjectId("58d0d84ebae387d0c1e8d871"), "x" : 999, "y" : 3 }
- 不存在則創(chuàng)建
> db.user.find({x:1})
> db.user.update({x:1},{x:2},true)
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("58d0da6e34123eac45d857ac")
})
> db.user.find()
{ "_id" : ObjectId("58d0d844bae387d0c1e8d870"), "x" : 999 }
{ "_id" : ObjectId("58d0d84ebae387d0c1e8d871"), "x" : 999, "y" : 3 }
{ "_id" : ObjectId("58d0da6e34123eac45d857ac"), "x" : 2 }
- 更新多條
mongodb默認(rèn)是更新一條,據(jù)說是為了防止一時(shí)大意,
而且更新多條不允許更新整個(gè),只能使用$set關(guān)鍵字
> db.user.find()
{ "_id" : ObjectId("58d0dde9bae387d0c1e8d873"), "x" : 1, "y" : 2, "z" : 1 }
{ "_id" : ObjectId("58d0ddeabae387d0c1e8d874"), "x" : 1, "y" : 1, "z" : 1 }
{ "_id" : ObjectId("58d0ddebbae387d0c1e8d875"), "x" : 1, "y" : 1, "z" : 1 }
> db.user.update({x:1},{$set:{y:2}},false,true)
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 2 })
> db.user.find()
{ "_id" : ObjectId("58d0dde9bae387d0c1e8d873"), "x" : 1, "y" : 2, "z" : 1 }
{ "_id" : ObjectId("58d0ddeabae387d0c1e8d874"), "x" : 1, "y" : 2, "z" : 1 }
{ "_id" : ObjectId("58d0ddebbae387d0c1e8d875"), "x" : 1, "y" : 2, "z" : 1 }
remove
- 刪除某個(gè)collection所有內(nèi)容
> db.user.remove({})
WriteResult({ "nRemoved" : 102 })
- 刪除某個(gè)
> db.user.remove({x:3})
WriteResult({ "nRemoved" : 1 })
find
- 查找所有
> db.user.find()
{ "_id" : ObjectId("58d0cebcbae387d0c1e8d80a"), "username" : "zhangsan" }
{ "_id" : ObjectId("58d0d36abae387d0c1e8d80b"), "x" : 1, "y" : 2, "z" : 3 }
{ "_id" : ObjectId("58d0d37dbae387d0c1e8d80c"), "x" : 1, "y" : 2, "z" : 3 }
- 有條件查找
> db.user.find({x:1})
{ "_id" : ObjectId("58d0d36abae387d0c1e8d80b"), "x" : 1, "y" : 2, "z" : 3 }
{ "_id" : ObjectId("58d0d37dbae387d0c1e8d80c"), "x" : 1, "y" : 2, "z" : 3 }
- 查找總數(shù)目
> db.user.find().count()
102
- 分頁,排序
> db.user.find().skip(5).limit(3).sort({x:1})
{ "_id" : ObjectId("58d0d453bae387d0c1e8d80f"), "x" : 3, "y" : 30, "z" : 300 }
{ "_id" : ObjectId("58d0d453bae387d0c1e8d810"), "x" : 4, "y" : 40, "z" : 400 }
{ "_id" : ObjectId("58d0d453bae387d0c1e8d811"), "x" : 5, "y" : 50, "z" : 500 }
個(gè)人博客:http://notes.xbug.site