mongodb基本命令和CURD操作

发布于 2019-09-26 作者 风铃 51次 浏览 版块 前端

一,基本命令:

1,查看所有数据库:show dbs                 

2,选择数据库  use dbName,如果数据库不存在,则会新建数据库 

3,查看所有数据库相关的操作(类似javascript函数) db.help()

使用db.help()得到数据库所有操作如下: > db.help()
DB methods:
        db.addUser(userDocument)
        db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.r
unCommand(...) ]
        db.auth(username, password)
        db.cloneDatabase(fromhost) copy database
        db.commandHelp(name) returns the help for the command
        db.copyDatabase(fromdb, todb, fromhost)
        db.createCollection(name, { size : ..., capped : ..., max : ... } ) 创建集合
        db.currentOp() displays currently executing operations in the db
        db.dropDatabase() drop database
        db.eval(func, args) run code server-side
        db.fsyncLock() flush data to disk and lock server for backups
        db.fsyncUnlock() unlocks server following a db.fsyncLock()
        db.getCollection(cname) same as db['cname'] or db.cname
        db.getCollectionNames() 得到数据库中所有集合的名字
        db.getLastError() - just returns the err msg string
        db.getLastErrorObj() - return full status object
        db.getMongo() get the server connection object
        db.getMongo().setSlaveOk() allow queries on a replication slave server
        db.getName() 得到数据库名字
        db.getPrevError()
        db.getProfilingLevel() - deprecated
        db.getProfilingStatus() - returns if profiling is on and slow threshold
        db.getReplicationInfo()
        db.getSiblingDB(name) get the db at the same server as this one
        db.hostInfo() get details about the server's host
        db.isMaster() check replica primary status
        db.killOp(opid) kills the current operation in the db
        db.listCommands() lists all the db commands
        db.loadServerScripts() loads all the scripts in db.system.js
        db.logout()
        db.printCollectionStats() 显示所有集合信息
        db.printReplicationInfo()
        db.printShardingStatus() 查看分片信息
        db.printSlaveReplicationInfo()
        db.removeUser(username)
        db.repairDatabase()
        db.resetError()
        db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into { cmdObj
 : 1 }
        db.serverStatus() 查看服务器信息
        db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all
        db.setVerboseShell(flag) display extra information in shell output
        db.shutdownServer()
        db.stats() 查看数据库相关信息
        db.version() current version of the server
>


4,删除数据库  db.dropDatabase()

5,查看所有集合  show collections

6,新建集合   db.collectionName.insert({_id:123})  当向不存在的集合中插入对象时,会新建集合对象  也可以使用db.createCollection("users")创建集合

7,删除集合  db.collectionName.drop()  删除集合

8,显示集合所有对象: db.collectionName.find()

9, 删除集合所有对象: db.collectionName.remove()  只是删除集合中的所有数据,但是集合和相应的索引都还是存在的

二 mongdb  的CURD 相关操作

1,添加操作

1.1,单个添加:

> use mogo
switched to db mogo
> db.users.insert({_id:123,name:"123"})
> db.users.find()
{ "_id" : 123, "name" : "123" }

switched to db mogo

> db.users.insert({_id:123,name:"123"})

> db.users.find()

{ "_id" : 123, "name" : "123" }

switched to db mogo

> db.users.insert({_id:123,name:"123"})

> db.users.find()

{ "_id" : 123, "name" : "123" }

switched to db mogo

> db.users.insert({_id:123,name:"123"})

> db.users.find()

{ "_id" : 123, "name" : "123" }

1.2 ,批量添加(使用javascript for 循环添加):

> for(var i=0;i<5;i++){

... db.users.insert({

... _id:i,name:"name"+i,sex:20+i,address:"address"+i,tell:"134567890"+i}

... )}

> db.users.find()

{ "_id" : 123, "name" : "123" }

{ "_id" : 0, "name" : "name0", "sex" : 20, "address" : "address0", "tell" : "1345678900" }

{ "_id" : 1, "name" : "name1", "sex" : 21, "address" : "address1", "tell" : "1345678901" }

{ "_id" : 2, "name" : "name2", "sex" : 22, "address" : "address2", "tell" : "1345678902" }

{ "_id" : 3, "name" : "name3", "sex" : 23, "address" : "address3", "tell" : "1345678903" }

{ "_id" : 4, "name" : "name4", "sex" : 24, "address" : "address4", "tell" : "1345678904" }

>  

1.3,insert() 和save():如果使用insert()插入的时候_id 已经存在会报错,而如果使用save()则会修改信息

> db.users.find()

{ "_id" : 123, "name" : "123" } 修改前的值

{ "_id" : 0, "name" : "name0", "sex" : 20, "address" : "address0", "tell" : "1345678900" }

{ "_id" : 1, "name" : "name1", "sex" : 21, "address" : "address1", "tell" : "1345678901" }

{ "_id" : 2, "name" : "name2", "sex" : 22, "address" : "address2", "tell" : "1345678902" }

{ "_id" : 3, "name" : "name3", "sex" : 23, "address" : "address3", "tell" : "1345678903" }

{ "_id" : 4, "name" : "name4", "sex" : 24, "address" : "address4", "tell" : "1345678904" }

> db.users.insert({_id:123,name:"12345"})

E11000 duplicate key error index: mogo.users.$_id_  dup key: { : 123.0 } 主键重复错误

> db.users.save({_id:123,name:"12345"})

> db.users.find()

{ "_id" : 0, "name" : "name0", "sex" : 20, "address" : "address0", "tell" : "1345678900" }

{ "_id" : 1, "name" : "name1", "sex" : 21, "address" : "address1", "tell" : "1345678901" }

{ "_id" : 2, "name" : "name2", "sex" : 22, "address" : "address2", "tell" : "1345678902" }

{ "_id" : 3, "name" : "name3", "sex" : 23, "address" : "address3", "tell" : "1345678903" }

{ "_id" : 4, "name" : "name4", "sex" : 24, "address" : "address4", "tell" : "1345678904" }

{ "_id" : 123, "name" : "12345" } save()方法修改之后的值

> 

2,删除操作  

2.1:条件删除 db.collectionNmae.remove({})        {}放删除的条件         

> db.users.find()  

{ "_id" : 0, "name" : "name0", "sex" : 20, "address" : "address0", "tell" : "1345678900" }

{ "_id" : 1, "name" : "name1", "sex" : 21, "address" : "address1", "tell" : "1345678901" }

{ "_id" : 2, "name" : "name2", "sex" : 22, "address" : "address2", "tell" : "1345678902" }

{ "_id" : 3, "name" : "name3", "sex" : 23, "address" : "address3", "tell" : "1345678903" }

{ "_id" : 4, "name" : "name4", "sex" : 24, "address" : "address4", "tell" : "1345678904" }

{ "_id" : 123, "age" : "12345" }

> db.users.remove({_id:123})  //根据id删除

> db.users.find()

{ "_id" : 0, "name" : "name0", "sex" : 20, "address" : "address0", "tell" : "1345678900" }

{ "_id" : 1, "name" : "name1", "sex" : 21, "address" : "address1", "tell" : "1345678901" }

{ "_id" : 2, "name" : "name2", "sex" : 22, "address" : "address2", "tell" : "1345678902" }

{ "_id" : 3, "name" : "name3", "sex" : 23, "address" : "address3", "tell" : "1345678903" }

{ "_id" : 4, "name" : "name4", "sex" : 24, "address" : "address4", "tell" : "1345678904" }

>

         2.2:全部删除db.collectionName.remove()


3,修改操作

3.1  :update 操作 db.collectionName.update({修改条件},{修改后的值})

> db.users.find()

{ "_id" : 0, "name" : "name0", "sex" : 20, "address" : "address0", "tell" : "1345678900" }

{ "_id" : 1, "name" : "name1", "sex" : 21, "address" : "address1", "tell" : "1345678901" }

{ "_id" : 2, "name" : "name2", "sex" : 22, "address" : "address2", "tell" : "1345678902" }

{ "_id" : 3, "name" : "name3", "sex" : 23, "address" : "address3", "tell" : "1345678903" }

{ "_id" : 4, "name" : "name4", "sex" : 24, "address" : "address4", "tell" : "1345678904" }

> db.users.update(

... {name:"name0"}, //修改条件

... {name:"name00"}) //修改后的值

> db.users.find()

{ "_id" : 0, "name" : "name00" }   可以看到修改之后的值就是我们update函数中的值,其它的属性都没有了

{ "_id" : 1, "name" : "name1", "sex" : 21, "address" : "address1", "tell" : "1345678901" }

{ "_id" : 2, "name" : "name2", "sex" : 22, "address" : "address2", "tell" : "1345678902" }

{ "_id" : 3, "name" : "name3", "sex" : 23, "address" : "address3", "tell" : "1345678903" }

{ "_id" : 4, "name" : "name4", "sex" : 24, "address" : "address4", "tell" : "1345678904" }

>

如果需要保留其它的属性,需要使用$set 修改器:

> db.users.find()

{ "_id" : 0, "name" : "name00" }

{ "_id" : 1, "name" : "name1", "sex" : 21, "address" : "address1", "tell" : "1345678901" }

{ "_id" : 2, "name" : "name2", "sex" : 22, "address" : "address2", "tell" : "1345678902" }

{ "_id" : 3, "name" : "name3", "sex" : 23, "address" : "address3", "tell" : "1345678903" }

{ "_id" : 4, "name" : "name4", "sex" : 24, "address" : "address4", "tell" : "1345678904" }

> db.users.update(

... {name:"name1"},

... {$set:{name:"name11"}})    //使用$set 修改器将name为“name1”的修改成“name11”

> db.users.find()

{ "_id" : 0, "name" : "name00" }

{ "_id" : 1, "address" : "address1", "name" : "name11", "sex" : 21, "tell" : "1345678901" }          原来的其他属性保留

{ "_id" : 2, "name" : "name2", "sex" : 22, "address" : "address2", "tell" : "1345678902" }

{ "_id" : 3, "name" : "name3", "sex" : 23, "address" : "address3", "tell" : "1345678903" }

{ "_id" : 4, "name" : "name4", "sex" : 24, "address" : "address4", "tell" : "1345678904" }

> db.users.update( {name:"name11"}, {$set:{country:"country"}})          如果$set 中的属性原来的文档中没有,则会添加到原来的文档中

> db.users.find()

{ "_id" : 0, "name" : "name00" }

{ "_id" : 2, "name" : "name2", "sex" : 22, "address" : "address2", "tell" : "1345678902" }

{ "_id" : 3, "name" : "name3", "sex" : 23, "address" : "address3", "tell" : "1345678903" }

{ "_id" : 4, "name" : "name4", "sex" : 24, "address" : "address4", "tell" : "1345678904" }

{ "_id" : 1, "address" : "address1", "country" : "country", "name" : "name11", "sex" : 21, "tell" :

"1345678901" }            在原来的属性的基础之上添加了新的country属

>

3.2 保存或则修改操作  db.collectionName({条件},{新的值},true)

> db.users.find()

{ "_id" : 0, "name" : "name0", "sex" : 20, "address" : "address0", "tell" : "1345678900" }

{ "_id" : 1, "name" : "name1", "sex" : 21, "address" : "address1", "tell" : "1345678901" }

{ "_id" : 2, "name" : "name2", "sex" : 22, "address" : "address2", "tell" : "1345678902" }

{ "_id" : 3, "name" : "name3", "sex" : 23, "address" : "address3", "tell" : "1345678903" }

{ "_id" : 4, "name" : "name4", "sex" : 24, "address" : "address4", "tell" : "1345678904" }


> db.users.update(

... {name:"name00"},    

... {name:"name000",age:90,address:"address9"},

... true)      //如果查询条件的值不存在,则将新的值插入到文档中

> db.users.find()

{ "_id" : 0, "name" : "name0", "sex" : 20, "address" : "address0", "tell" : "1345678900" }

{ "_id" : 1, "name" : "name1", "sex" : 21, "address" : "address1", "tell" : "1345678901" }

{ "_id" : 2, "name" : "name2", "sex" : 22, "address" : "address2", "tell" : "1345678902" }

{ "_id" : 3, "name" : "name3", "sex" : 23, "address" : "address3", "tell" : "1345678903" }

{ "_id" : 4, "name" : "name4", "sex" : 24, "address" : "address4", "tell" : "1345678904" }

{ "_id" : ObjectId("54fbe5a29f149608709573cc"), "name" : "name000", "age" : 90, "address" : "address

9" }      新增加的文档

3.3 批量修改操作

> db.users.find()

{ "_id" : 0, "name" : "name0", "sex" : 20, "address" : "address0", "tell" : "1345678900" }

{ "_id" : 1, "name" : "name1", "sex" : 21, "address" : "address1", "tell" : "1345678901" }

{ "_id" : 2, "name" : "name2", "sex" : 22, "address" : "address2", "tell" : "1345678902" }

{ "_id" : 3, "name" : "name3", "sex" : 23, "address" : "address3", "tell" : "1345678903" }

{ "_id" : 4, "name" : "name4", "sex" : 24, "address" : "address4", "tell" : "1345678904" }

{ "_id" : ObjectId("54fbe5a29f149608709573cc"), "name" : "name000", "age" : 90, "address" : "address

9" }

> db.users.update(

... {name:/^name/},     //使用正则表达式  ,修改name是以“name”开头的文档

... {name:"new name"})

> db.users.find()

{ "_id" : 0, "name" : "new name" }       结果只有第一条满足的被修改了

{ "_id" : 1, "name" : "name1", "sex" : 21, "address" : "address1", "tell" : "1345678901" }

{ "_id" : 2, "name" : "name2", "sex" : 22, "address" : "address2", "tell" : "1345678902" }

{ "_id" : 3, "name" : "name3", "sex" : 23, "address" : "address3", "tell" : "1345678903" }

{ "_id" : 4, "name" : "name4", "sex" : 24, "address" : "address4", "tell" : "1345678904" }

{ "_id" : ObjectId("54fbe5a29f149608709573cc"), "name" : "name000", "age" : 90, "address" : "address

9" }

>

使用批量修改

> db.users.find()

{ "_id" : 0, "name" : "new name" }

{ "_id" : 1, "name" : "name1", "sex" : 21, "address" : "address1", "tell" : "1345678901" }

{ "_id" : 2, "name" : "name2", "sex" : 22, "address" : "address2", "tell" : "1345678902" }

{ "_id" : 3, "name" : "name3", "sex" : 23, "address" : "address3", "tell" : "1345678903" }

{ "_id" : 4, "name" : "name4", "sex" : 24, "address" : "address4", "tell" : "1345678904" }

{ "_id" : ObjectId("54fbe5a29f149608709573cc"), "name" : "name000", "age" : 90, "address" : "address

9" }

> db.users.update(

... {name:/^name/},

... {name:"the new name"},

... false,

... true)    

multi update only works with $ operators      批量修改只能使用修改器

> db.users.update(

... {name:/^name/},

... {$set:{name:"the new name"}},         使用$set 修改器

... false,

... true)

> db.users.find()

{ "_id" : 0, "name" : "new name" }

{ "_id" : 4, "address" : "address4", "name" : "the new name", "sex" : 24, "tell" : "1345678904" }

{ "_id" : ObjectId("54fbe5a29f149608709573cc"), "address" : "address9", "age" : 90, "name" : "the ne

w name" }

{ "_id" : 1, "address" : "address1", "name" : "the new name", "sex" : 21, "tell" : "1345678901" }

{ "_id" : 2, "address" : "address2", "name" : "the new name", "sex" : 22, "tell" : "1345678902" }

{ "_id" : 3, "address" : "address3", "name" : "the new name", "sex" : 23, "tell" : "1345678903" }

>

3.4使用修改器完成修改操作

3.4.1 $set修改器,如果新的属性不存在则会添加到文档,如果存在则会修改

> db.users.find()

{ "_id" : 0, "name" : "name0", "sex" : 20, "address" : "address0", "tell" : "1345678900" }

{ "_id" : 1, "name" : "name1", "sex" : 21, "address" : "address1", "tell" : "1345678901" }

{ "_id" : 2, "name" : "name2", "sex" : 22, "address" : "address2", "tell" : "1345678902" }

{ "_id" : 3, "name" : "name3", "sex" : 23, "address" : "address3", "tell" : "1345678903" }

{ "_id" : 4, "name" : "name4", "sex" : 24, "address" : "address4", "tell" : "1345678904" }

> db.users.update(

... {name:/^name/},

... {$set:{name:"new name",country:"country"}},     使用$set 修改name属性,添加country属性

... false,

... true)

> db.users.find()

{ "_id" : 0, "address" : "address0", "country" : "country", "name" : "new name", "sex" : 20, "tell"

: "1345678900" }

{ "_id" : 1, "address" : "address1", "country" : "country", "name" : "new name", "sex" : 21, "tell"

: "1345678901" }

{ "_id" : 2, "address" : "address2", "country" : "country", "name" : "new name", "sex" : 22, "tell"

: "1345678902" }

{ "_id" : 3, "address" : "address3", "country" : "country", "name" : "new name", "sex" : 23, "tell"

: "1345678903" }

{ "_id" : 4, "address" : "address4", "country" : "country", "name" : "new name", "sex" : 24, "tell"

: "1345678904" }

>

 3.4.2使用 $unset 修改器   将文档中的部分属性删除

> db.users.find()

{ "_id" : 0, "address" : "address0", "name" : "new name", "sex" : 20, "tell" : "1345678900" }

{ "_id" : 1, "address" : "address1", "name" : "new name", "sex" : 21, "tell" : "1345678901" }

{ "_id" : 2, "address" : "address2", "name" : "new name", "sex" : 22, "tell" : "1345678902" }

{ "_id" : 3, "address" : "address3", "name" : "new name", "sex" : 23, "tell" : "1345678903" }

{ "_id" : 4, "address" : "address4", "name" : "new name", "sex" : 24, "tell" : "1345678904" }

> db.users.update( {name:/^new/}, {$unset:{name:"123"}}, false ,true)  //将name属性去掉,至于name的值取什么都可以

> db.users.find()

{ "_id" : 0, "address" : "address0", "sex" : 20, "tell" : "1345678900" }

{ "_id" : 1, "address" : "address1", "sex" : 21, "tell" : "1345678901" }

{ "_id" : 2, "address" : "address2", "sex" : 22, "tell" : "1345678902" }

{ "_id" : 3, "address" : "address3", "sex" : 23, "tell" : "1345678903" }

{ "_id" : 4, "address" : "address4", "sex" : 24, "tell" : "1345678904" }

>

    3.4.3 使用$inc 修改器可以完成对数字类型的加减操作

> db.users.find() { "_id" : 0, "address" : "address0", "sex" : 20, "tell" : "1345678900" } { "_id" : 1, "address" : "address1", "sex" : 21, "tell" : "1345678901" } { "_id" : 2, "address" : "address2", "sex" : 22, "tell" : "1345678902" } { "_id" : 3, "address" : "address3", "sex" : 23, "tell" : "1345678903" } { "_id" : 4, "address" : "address4", "sex" : 24, "tell" : "1345678904" } > db.users.update( ... {address:/^address/}, ... {$inc:{sex:1}},   //将所有的sex自增1 如果为负数则减少 ... false, ... true) > db.users.find() { "_id" : 0, "address" : "address0", "sex" : 21, "tell" : "1345678900" } { "_id" : 1, "address" : "address1", "sex" : 22, "tell" : "1345678901" } { "_id" : 2, "address" : "address2", "sex" : 23, "tell" : "1345678902" } { "_id" : 3, "address" : "address3", "sex" : 24, "tell" : "1345678903" } { "_id" : 4, "address" : "address4", "sex" : 25, "tell" : "1345678904" }

>

3.4.4. 使用$push 完成文档的添加 $pushAll 完成文档的批量添加  都只能操作数组

> db.users.find()

{ "_id" : 0, "address" : "address0", "sex" : 21, "tell" : "1345678900" }

{ "_id" : 1, "address" : "address1", "sex" : 22, "tell" : "1345678901" }

{ "_id" : 2, "address" : "address2", "sex" : 23, "tell" : "1345678902" }

{ "_id" : 3, "address" : "address3", "sex" : 24, "tell" : "1345678903" }

{ "_id" : 4, "address" : "address4", "sex" : 25, "tell" : "1345678904" }

> db.users.update(

... {address:/^address/},

... {$push:{books:"java"}},  //如果books属性不存在则添加到文档

... false,

... true)

> db.users.find()

{ "_id" : 0, "address" : "address0", "books" : [  "java" ], "sex" : 21, "tell" : "1345678900" }

{ "_id" : 1, "address" : "address1", "books" : [  "java" ], "sex" : 22, "tell" : "1345678901" }

{ "_id" : 2, "address" : "address2", "books" : [  "java" ], "sex" : 23, "tell" : "1345678902" }

{ "_id" : 3, "address" : "address3", "books" : [  "java" ], "sex" : 24, "tell" : "1345678903" }

{ "_id" : 4, "address" : "address4", "books" : [  "java" ], "sex" : 25, "tell" : "1345678904" }

> db.users.update( {address:/^address/}, {$push:{books:"c++"}}, false, true) 如果存在则在而且是数组则后面添加,如果属性存在但是不是数组,则操作报错

> db.users.find()

{ "_id" : 0, "address" : "address0", "books" : [  "java",  "c++" ], "sex" : 21, "tell" : "1345678900

" }

{ "_id" : 1, "address" : "address1", "books" : [  "java",  "c++" ], "sex" : 22, "tell" : "1345678901

" }

{ "_id" : 2, "address" : "address2", "books" : [  "java",  "c++" ], "sex" : 23, "tell" : "1345678902

" }

{ "_id" : 3, "address" : "address3", "books" : [  "java",  "c++" ], "sex" : 24, "tell" : "1345678903

" }

{ "_id" : 4, "address" : "address4", "books" : [  "java",  "c++" ], "sex" : 25, "tell" : "1345678904

" }

>

3.4.5. 使用$pull 完成文档的内容删除 $pullAll 完成文档的批量删除  都只能操作数组

> db.users.find()

{ "_id" : 0, "address" : "address0", "books" : [  "java",  "c++" ], "sex" : 21, "tell" : "1345678900

" }

{ "_id" : 1, "address" : "address1", "books" : [  "java",  "c++" ], "sex" : 22, "tell" : "1345678901

" }

{ "_id" : 2, "address" : "address2", "books" : [  "java",  "c++" ], "sex" : 23, "tell" : "1345678902

" }

{ "_id" : 3, "address" : "address3", "books" : [  "java",  "c++" ], "sex" : 24, "tell" : "1345678903

" }

{ "_id" : 4, "address" : "address4", "books" : [  "java",  "c++" ], "sex" : 25, "tell" : "1345678904

" }

> db.users.update(

... {address:/^address/},

... {$pull:{books:"java"}},     //使用方式跟$push 一样作用相反

... false

... ,true)

> db.users.find()

{ "_id" : 0, "address" : "address0", "books" : [  "c++" ], "sex" : 21, "tell" : "1345678900" }

{ "_id" : 1, "address" : "address1", "books" : [  "c++" ], "sex" : 22, "tell" : "1345678901" }

{ "_id" : 2, "address" : "address2", "books" : [  "c++" ], "sex" : 23, "tell" : "1345678902" }

{ "_id" : 3, "address" : "address3", "books" : [  "c++" ], "sex" : 24, "tell" : "1345678903" }

{ "_id" : 4, "address" : "address4", "books" : [  "c++" ], "sex" : 25, "tell" : "1345678904" }

>

3.4.6使用$addToSet 想数组中添加,跟$push不同的是,使用$addToSet的时候如果属性已经存在则不添加,不存在才添加

> db.users.find()

{ "_id" : 0, "address" : "address0", "books" : [  "c++" ], "sex" : 21, "tell" : "1345678900" }

{ "_id" : 1, "address" : "address1", "books" : [  "c++" ], "sex" : 22, "tell" : "1345678901" }

{ "_id" : 2, "address" : "address2", "books" : [  "c++" ], "sex" : 23, "tell" : "1345678902" }

{ "_id" : 3, "address" : "address3", "books" : [  "c++" ], "sex" : 24, "tell" : "1345678903" }

{ "_id" : 4, "address" : "address4", "books" : [  "c++" ], "sex" : 25, "tell" : "1345678904" }

> db.users.update(

... {address:/address/},

... {$addToSet:{books:"c++"}}, //  由于集合中已经有了c++所以不会添加到集合中

... false,

... true)

> db.users.find()

{ "_id" : 0, "address" : "address0", "books" : [  "c++" ], "sex" : 21, "tell" : "1345678900" }

{ "_id" : 1, "address" : "address1", "books" : [  "c++" ], "sex" : 22, "tell" : "1345678901" }

{ "_id" : 2, "address" : "address2", "books" : [  "c++" ], "sex" : 23, "tell" : "1345678902" }

{ "_id" : 3, "address" : "address3", "books" : [  "c++" ], "sex" : 24, "tell" : "1345678903" }

{ "_id" : 4, "address" : "address4", "books" : [  "c++" ], "sex" : 25, "tell" : "1345678904" }

> db.users.update( {address:/address/}, {$addToSet:{books:"java"}}, false, true) //集合中没有 java 所以添加到集合中

> db.users.find()

{ "_id" : 0, "address" : "address0", "books" : [  "c++",  "java" ], "sex" : 21, "tell" : "1345678900

" }

{ "_id" : 1, "address" : "address1", "books" : [  "c++",  "java" ], "sex" : 22, "tell" : "1345678901

" }

{ "_id" : 2, "address" : "address2", "books" : [  "c++",  "java" ], "sex" : 23, "tell" : "1345678902

" }

{ "_id" : 3, "address" : "address3", "books" : [  "c++",  "java" ], "sex" : 24, "tell" : "1345678903

" }

{ "_id" : 4, "address" : "address4", "books" : [  "c++",  "java" ], "sex" : 25, "tell" : "1345678904

" }

>

3.4.7 使用$pop 取集合中的元素  

> db.users.find()

{ "_id" : 0, "address" : "address0", "books" : [  "c++",  "java" ], "sex" : 21, "tell" : "1345678900

" }

{ "_id" : 1, "address" : "address1", "books" : [  "c++",  "java" ], "sex" : 22, "tell" : "1345678901

" }

{ "_id" : 2, "address" : "address2", "books" : [  "c++",  "java" ], "sex" : 23, "tell" : "1345678902

" }

{ "_id" : 3, "address" : "address3", "books" : [  "c++",  "java" ], "sex" : 24, "tell" : "1345678903

" }

{ "_id" : 4, "address" : "address4", "books" : [  "c++",  "java" ], "sex" : 25, "tell" : "1345678904

" }

> db.users.update(

... {_id:0},

... {$pop:{books:1}})    //去除集合中的最后一个

> db.users.find()

{ "_id" : 0, "address" : "address0", "books" : [  "c++" ], "sex" : 21, "tell" : "1345678900" }

{ "_id" : 1, "address" : "address1", "books" : [  "c++",  "java" ], "sex" : 22, "tell" : "1345678901

" }

{ "_id" : 2, "address" : "address2", "books" : [  "c++",  "java" ], "sex" : 23, "tell" : "1345678902

" }

{ "_id" : 3, "address" : "address3", "books" : [  "c++",  "java" ], "sex" : 24, "tell" : "1345678903

" }

{ "_id" : 4, "address" : "address4", "books" : [  "c++",  "java" ], "sex" : 25, "tell" : "1345678904

" }

> db.users.update( {_id:1}, {$pop:{books:-1}})  //去除集合中的第一个

> db.users.find()

{ "_id" : 0, "address" : "address0", "books" : [  "c++" ], "sex" : 21, "tell" : "1345678900" }

{ "_id" : 1, "address" : "address1", "books" : [  "java" ], "sex" : 22, "tell" : "1345678901" }

{ "_id" : 2, "address" : "address2", "books" : [  "c++",  "java" ], "sex" : 23, "tell" : "1345678902

" }

{ "_id" : 3, "address" : "address3", "books" : [  "c++",  "java" ], "sex" : 24, "tell" : "1345678903

" }

{ "_id" : 4, "address" : "address4", "books" : [  "c++",  "java" ], "sex" : 25, "tell" : "1345678904

" }

>

4,查询操作

4.1:查询所有:   db.collectionName.find()

4.2: 条件查询所有属性db.collectionName.find({查询条件})

4.3:条件查询部分属性:

> db.users.find( {_id:0}, {_id:0,address:1,books:1})            //属性为0表示不查询为1表示查询   默认情况会查询id

{ "address" : "address0", "books" : [  "c++" ] }

>

4.4 使用  <($lt)  <=($lte)    >($gt)  >=($gte)   !=($ne)

> db.users.find()

{ "_id" : 0, "address" : "address0", "books" : [  "c++" ], "sex" : 21, "tell" : "1345678900" }

{ "_id" : 1, "address" : "address1", "books" : [  "java" ], "sex" : 22, "tell" : "1345678901" }

{ "_id" : 2, "address" : "address2", "books" : [  "c++",  "java" ], "sex" : 23, "tell" : "1345678902

" }

{ "_id" : 3, "address" : "address3", "books" : [  "c++",  "java" ], "sex" : 24, "tell" : "1345678903

" }

{ "_id" : 4, "address" : "address4", "books" : [  "c++",  "java" ], "sex" : 25, "tell" : "1345678904

" }

> db.users.find( {sex:{$gt:22,$lt:25}}, {_id:0,address:1,books:1,sex:1})  //  查找性别  大于 22小于25 的 用户  显示部分属性

{ "address" : "address2", "books" : [  "c++",  "java" ], "sex" : 23 }

{ "address" : "address3", "books" : [  "c++",  "java" ], "sex" : 24 }

>

> db.users.find( {address:{$ne:"address0"}}, {_id:0,address:1,books:1,sex:1})  //使用 不等于条件查找 { "address" : "address1", "books" : [  "java" ], "sex" : 22 } { "address" : "address2", "books" : [  "c++",  "java" ], "sex" : 23 } { "address" : "address3", "books" : [  "c++",  "java" ], "sex" : 24 } { "address" : "address4", "books" : [  "c++",  "java" ], "sex" : 25 }

>

4.5 使用  and  or   

> db.users.find()

{ "_id" : 0, "address" : "address0", "books" : [  "c++" ], "sex" : 21, "tell" : "1345678900" }

{ "_id" : 1, "address" : "address1", "books" : [  "java" ], "sex" : 22, "tell" : "1345678901" }

{ "_id" : 2, "address" : "address2", "books" : [  "c++",  "java" ], "sex" : 23, "tell" : "1345678902

" }

{ "_id" : 3, "address" : "address3", "books" : [  "c++",  "java" ], "sex" : 24, "tell" : "1345678903

" }

{ "_id" : 4, "address" : "address4", "books" : [  "c++",  "java" ], "sex" : 25, "tell" : "1345678904

" }

> db.users.find( {_id:{$gte:2},sex:{$lte:24}}) 当使用多个条件的时候 默认and 链接

{ "_id" : 2, "address" : "address2", "books" : [  "c++",  "java" ], "sex" : 23, "tell" : "1345678902

" }

{ "_id" : 3, "address" : "address3", "books" : [  "c++",  "java" ], "sex" : 24, "tell" : "1345678903

" }


> db.users.find()

{ "_id" : 0, "address" : "address0", "books" : [  "c++" ], "sex" : 21, "tell" : "1345678900" }

{ "_id" : 1, "address" : "address1", "books" : [  "java" ], "sex" : 22, "tell" : "1345678901" }

{ "_id" : 2, "address" : "address2", "books" : [  "c++",  "java" ], "sex" : 23, "tell" : "1345678902

" }

{ "_id" : 3, "address" : "address3", "books" : [  "c++",  "java" ], "sex" : 24, "tell" : "1345678903

" }

{ "_id" : 4, "address" : "address4", "books" : [  "c++",  "java" ], "sex" : 25, "tell" : "1345678904

" }

> db.users.find( 使用or链接条件

... {$or:[

... {_id:{$gt:3}},

... {sex:{$lt:24}}

... ]})

{ "_id" : 0, "address" : "address0", "books" : [  "c++" ], "sex" : 21, "tell" : "1345678900" }

{ "_id" : 1, "address" : "address1", "books" : [  "java" ], "sex" : 22, "tell" : "1345678901" }

{ "_id" : 2, "address" : "address2", "books" : [  "c++",  "java" ], "sex" : 23, "tell" : "1345678902

" }

{ "_id" : 4, "address" : "address4", "books" : [  "c++",  "java" ], "sex" : 25, "tell" : "1345678904

" }

>

4.6 使用$in   $nin

> db.users.find()

{ "_id" : 0, "address" : "address0", "books" : [  "c++" ], "sex" : 21, "tell" : "1345678900" }

{ "_id" : 1, "address" : "address1", "books" : [  "java" ], "sex" : 22, "tell" : "1345678901" }

{ "_id" : 2, "address" : "address2", "books" : [  "c++",  "java" ], "sex" : 23, "tell" : "1345678902

" }

{ "_id" : 3, "address" : "address3", "books" : [  "c++",  "java" ], "sex" : 24, "tell" : "1345678903

" }

{ "_id" : 4, "address" : "address4", "books" : [  "c++",  "java" ], "sex" : 25, "tell" : "1345678904

" }

> db.users.find(

... {_id:{$in:[0,3,4]}})

{ "_id" : 0, "address" : "address0", "books" : [  "c++" ], "sex" : 21, "tell" : "1345678900" }

{ "_id" : 3, "address" : "address3", "books" : [  "c++",  "java" ], "sex" : 24, "tell" : "1345678903

" }

{ "_id" : 4, "address" : "address4", "books" : [  "c++",  "java" ], "sex" : 25, "tell" : "1345678904

" }

> db.users.find(

... {_id:{$nin:[0,3,4]}})

{ "_id" : 1, "address" : "address1", "books" : [  "java" ], "sex" : 22, "tell" : "1345678901" }

{ "_id" : 2, "address" : "address2", "books" : [  "c++",  "java" ], "sex" : 23, "tell" : "1345678902

" }

>

收藏
暂无回复