1. 程式人生 > >mongodb---增刪改查

mongodb---增刪改查

(一)增(往集合中插入資料,即文件)

第一種插入方法:

往集合textCollection中插入資料 {”name“:”xiaoming“},直接用 db.集合名.insert():

> db.textCollection.insert({"name":"xiaoming"})
WriteResult({ "nInserted" : 1 })
>

檢視集合textCollection中的文件(資料):

> db.textCollection.find()
{ "_id" : ObjectId("5beb9f0c114e061388762deb"), "name" : "xiaoming" }
>

一次性插入多個數據:

> db.textCollection.insert([{a:1},{b:2}])

第二種插入方法:

先構建一個變數(document)表示要插入的資料,然後用  db.集合名.insert(變數)來插入:

> document=({name:"xiaohong",age:"21"})
{ "name" : "xiaohong", "age" : "21" }
> db.textCollection.insert(document)
WriteResult({ "nInserted" : 1 })
> db.textCollection.find()
{ "_id" : ObjectId("5beb9f0c114e061388762deb"), "name" : "xiaoming" }
{ "_id" : ObjectId("5bebea60127c93733825f9ff"), "name" : "xiaohong", "age" : "21" }
>

一次性插入多個數據:

> documents = ([{name:"xiaoming",age:21},{name:"xiaowen",age:22}])
> db.textCollection.insert(documents)

(二)刪

刪除集合textCollection中,所有name為xiaoming的文件:

> db.textCollection.remove({name:"xiaoming"})
WriteResult({ "nRemoved" : 2 })

第二行表示成功刪除了2條文件。

(三)查

(1)查詢整個集合:

db.textCollection.find():查詢整個textCollection集合:

> db.textCollection.find()
{ "_id" : ObjectId("5bec01c8127c93733825fa0a"), "name" : "xiaoming", "age" : 21 }
{ "_id" : ObjectId("5bec01c8127c93733825fa0b"), "name" : "xiaohong", "age" : 20 }
>

(2)db.集合名.find().pretty() ----格式化輸出:

> db.textCollection.find().pretty()
{
        "_id" : ObjectId("5bec01c8127c93733825fa0a"),
        "name" : "xiaoming",
        "age" : 21
}
{
        "_id" : ObjectId("5bec01c8127c93733825fa0b"),
        "name" : "xiaohong",
        "age" : 20
}
>

(3)條件並(AND)查詢:

查詢textCollection集合中age為21且name為xiaoming的文件:

> db.textCollection.find({"age":21,"name":"xiaoming"})
{ "_id" : ObjectId("5bec01c8127c93733825fa0a"), "name" : "xiaoming", "age" : 21 }
>

(4)條件或(OR)查詢:

查詢textCollection集合中age為20或21的文件:

用到 $or:表示“或”。

> db.textCollection.find({$or:[{"age":20},{"age":21}]})
{ "_id" : ObjectId("5bec01c8127c93733825fa0a"), "name" : "xiaoming", "age" : 21 }
{ "_id" : ObjectId("5bec01c8127c93733825fa0b"), "name" : "xiaohong", "age" : 20 }
{ "_id" : ObjectId("5bec0439127c93733825fa0c"), "name" : "xiaowen", "age" : 21 }
{ "_id" : ObjectId("5bec0439127c93733825fa0d"), "name" : "xiaogang", "age" : 20 }
>

(四)改

改即update,更新集合中的文件。

把textCollection集合中的包含name為xiaoming的資料的age欄位改成18:

db.textCollection.update({"name":"xiaoming"},{$set:{"age":18}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>

上面的方法只能修改第一個找到的資料,若有多個數據的name為xiaoming,且要把所有name為xiaoming的資料的age欄位改為18的話,要額外加上{multi:true}:

> db.textCollection.update({"name":"xiaoming"},{$set:{"age":18}},{multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.textCollection.find()
{ "_id" : ObjectId("5bec01c8127c93733825fa0a"), "name" : "xiaoming", "age" : 18 }
{ "_id" : ObjectId("5bec01c8127c93733825fa0b"), "name" : "xiaohong", "age" : 20 }
{ "_id" : ObjectId("5bec0439127c93733825fa0c"), "name" : "xiaowen", "age" : 21 }
{ "_id" : ObjectId("5bec0439127c93733825fa0d"), "name" : "xiaogang", "age" : 20 }
{ "_id" : ObjectId("5bec092c127c93733825fa0e"), "name" : "xiaoming", "age" : 18 }
>