標籤:mongodb基本操作
1.1. mongoDB基本操作1.1.1. 建立資料庫
文法:use [databasename]
> use admin
switched to db admin
> use rgf; #建立資料庫rgf
switched to db rgf
> db.person.insert({name:‘xuanxuan‘}) #在rgf中建立集合person並插入一行資料
WriteResult({ "nInserted" : 1 })
1.1.2. 查看所有資料庫
文法:show dbs
> show dbs;
admin (empty)
local 0.078GB
rgf 0.078GB
test 0.078GB
1.1.3. 給指定資料庫添加集合并添加記錄
文法:db.[documentName].insert({...})
> db.person.insert({age:‘10‘});
WriteResult({ "nInserted" : 1 })
>db.person.insert({age:‘10‘},{sex:‘male‘});
WriteResult({ "nInserted" : 1 })
1.1.4. 查看資料庫中的所有文檔
文法:showcollections;
> show collections;
person
system.indexes
1.1.5. 查詢指定文檔的資料
文法:
查詢所有:
db.[documnetName].find();
查詢第一條資料:
db.[documentName].findone()
> db.system.indexes.find()
{ "v" : 1, "key" : {"_id" : 1 }, "name" : "_id_", "ns" :"rgf.person" }
> db.person.find()
{ "_id" :ObjectId("549b31b7ff3a181677422f47"), "name" :"xuanxuan" }
{ "_id" :ObjectId("549b32ebff3a181677422f48"), "age" :"10" }
{ "_id" :ObjectId("549b3352ff3a181677422f49"), "age" :"10" }
> db.person.insert({age:‘20‘},{sex:‘male‘});
WriteResult({ "nInserted" : 1 })
> db.person.findone()
2014-12-25T05:58:14.270+0800 TypeError:Property ‘findone‘ of object rgf.person is not a function
> db.person.findOne()
{ "_id" : ObjectId("549b31b7ff3a181677422f47"),"name" : "xuanxuan" }
注意函數findOne中的O一定要大寫。
1.1.6. 更新文檔資料
文法:
db.[documentName].update({查詢條件},{更新內容})
例:
varp = db.person.findOne()
db.person.update(p,{name:"gaogao"})
>db.person.update({name:‘xuanxuan‘},{$set:{name:‘zhaoxing‘}})
WriteResult({ "nMatched" : 1,"nUpserted" : 0, "nModified" : 1 })
> db.person.findOne()
{ "_id" :ObjectId("549b31b7ff3a181677422f47"), "name" :"zhaoxing" }
>
> var p = db.person.findOne()
> p
{ "_id" :ObjectId("549b31b7ff3a181677422f47"), "name" :"zhaoxing" }
>db.person.update(p,{name:"gaogao"})
WriteResult({ "nMatched" : 1,"nUpserted" : 0, "nModified" : 1 })
> p
{ "_id" :ObjectId("549b31b7ff3a181677422f47"), "name" :"zhaoxing" }
> var p = db.person.findOne()
注意此時需要再一次賦值變數
> p
{ "_id" : ObjectId("549b31b7ff3a181677422f47"),"name" : "gaogao" }
>
{ "_id" :ObjectId("549b31b7ff3a181677422f47"), "name" :"gaogao2", "age" : 2 }
mongo預設更改第一條資料。
>db.person.update({age:"10"},{$set:{age:5}})
WriteResult({ "nMatched" : 1,"nUpserted" : 0, "nModified" : 1 })
> db.person.find()
{ "_id" :ObjectId("549b32ebff3a181677422f48"), "age" : 5 }
{ "_id" :ObjectId("549b3352ff3a181677422f49"), "age" :"10" }
{ "_id" :ObjectId("549b3508ff3a181677422f4a"), "age" :"20" }
{ "_id" :ObjectId("549b31b7ff3a181677422f47"), "name" : "gaogao2","age" : 2 }
>
1.1.7. 刪除文檔中的資料
文法:
db.[documentName].remove({...})
> db.person.find()
{ "_id" :ObjectId("549b32ebff3a181677422f48"), "age" : 5 }
{ "_id" :ObjectId("549b3352ff3a181677422f49"), "age" :"10" }
{ "_id" :ObjectId("549b3508ff3a181677422f4a"), "age" :"20" }
{ "_id" :ObjectId("549b31b7ff3a181677422f47"), "name" :"gaogao2", "age" : 2 }
> db.person.remove({age:"10"})
WriteResult({ "nRemoved" : 1 })
> db.person.find()
{ "_id" :ObjectId("549b32ebff3a181677422f48"), "age" : 5 }
{ "_id" :ObjectId("549b3508ff3a181677422f4a"), "age" :"20" }
{ "_id" :ObjectId("549b31b7ff3a181677422f47"), "name" :"gaogao2", "age" : 2 }
>
mongoDB基本操作(一)