mongoDB基本操作(一)

來源:互聯網
上載者:User

標籤: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基本操作(一)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.