首页 > 代码库 > mongoDB基本操作(一)

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