学习mongo系列(四) find().pretty() remove() 查询

一、find() 方法以非结构化的方式来显示所有文档。如果你需要以易读的方式来读取数据,可以使用 pretty() 方法,语法格式如下:db.collection_name.find().pretty()

  1、查看数据库下的所有的collection: show collections

    2、查看collection下的数据 db.collection_name.find().pretty()

  MongoDB数据更新可以使用update()函数。在执行remove()函数前先执行find()命令来判断执行的条件是否正确,这是一个比较好的习惯。

> db.user.save({name:"xxxxx",password:"11111"})
WriteResult({ "nInserted" : 1 })
> db.user.find().pretty()
{
"_id" : ObjectId("569476a63a18f4867aecbcd3"),
"name" : "xxxxx",
"password" : "11111"
}
> db.user.remove("name":"xxxxx")
2016-01-12T11:45:41.976+0800 E QUERY [thread1] SyntaxError: missing ) after a
rgument list @(shell):1:21

> db.user.remove({"name":"xxxxx"})
WriteResult({ "nRemoved" : 1 })
>还是没有注意检查括号

3、find()会返回匹配文档的所有列:

> db.user.find().pretty()
{
  "_id" : ObjectId("569476a63a18f4867aecbcd3"),
  "name" : "xxxxx",
  "password" : "11111"
}

4、返回指定字段和_id字段(默认会返回_id字段):

  db.user.find({"name":"xxxxx"},{name:1})

5、返回排除字段以外的所有字段:

  db.user.find({"name":"xxxxx"},{password:0}).pretty()

  所以4中要想不返回_id 字段,排除掉_id 字段即可:db.user.find({"name":"xxxxx"},{name:1, _id:0})

6、另外内嵌文档查询(点表示法):

  > db.tao.find({"SourceInfo.Platform": "Taobao"},{SourceInfo:1}).pretty(){

  "_id" : ObjectId("57467a880f281ce229632257"),
  "SourceInfo" : {
    "Platform" : "Taobao",
    "Link" : "https://item.taobao.com/item.htm?spm=a219r.lm869.14.14.n6XAO5&id=527138809962&ns=1&abbucket=17#detail",
    "Site" : "",
    "SiteID" : "",
    "ProductID" : "527138809962"
  }
}
{
  "_id" : ObjectId("57467ab60f281ce3adbd6b0a"),
  "SourceInfo" : {
    "Platform" : "Taobao",
    "Link" : "https://item.taobao.com/item.htm?spm=a219r.lm869.14.14.n6XAO5&id=527138809962&ns=1&abbucket=17#detail",
    "Site" : "",
    "SiteID" : "",
    "ProductID" : "527138809962"
  }
}

> db.tao.find({"SourceInfo.Platform": "Taobao"},{"SourceInfo.Platform":1}).pretty()
{
  "_id" : ObjectId("57467a880f281ce229632257"),
  "SourceInfo" : {
    "Platform" : "Taobao"
  }
}
{
  "_id" : ObjectId("57467ab60f281ce3adbd6b0a"),
  "SourceInfo" : {
    "Platform" : "Taobao"
  }

  7、当内嵌文档变得复杂后,如键的值为内嵌文档的数组,内嵌文档的匹配需要些许技巧,例如使用$elemMatch操作符。

    "SourceInfo" :

      [

    "Platform" : "Taobao",
    "Link" : "https://item.taobao.com/item.htm?spm=a219r.lm869.14.14.n6XAO5&id=527138809962&ns=1&abbucket=17#detail",
    "Site" : "",
    "SiteID" : "",
    "ProductID" : "527138809962"
  ]

> db.tao.find({},{"SourceInfo":{$elemMatch:{"Platform":"Taobao"}}, _id:0}).pretty(),【1】

 

 二、remove()

  在执行remove()函数前先执行find()命令来判断执行的条件是否正确,这是一个比较好的习惯。

> db.user.save({name:"xxxxx",password:"11111"})
  WriteResult({ "nInserted" : 1 })
> db.user.find().pretty()
{
  "_id" : ObjectId("569476a63a18f4867aecbcd3"),
  "name" : "xxxxx",
  "password" : "11111"
}
> db.user.remove("name":"xxxxx")
  2016-01-12T11:45:41.976+0800 E QUERY [thread1] SyntaxError: missing ) after a
  rgument list @(shell):1:21

> db.user.remove({"name":"xxxxx"})
  WriteResult({ "nRemoved" : 1 })
>还是没有注意检查括号

如果你想删除所有数据,可以使用以下方式(类似常规 SQL 的 truncate 命令):

db.user.remove()

三、MongoDB 与 RDBMS Where 语句比较(表格来自http://www.runoob.com/mongodb/mongodb-query.html)

如果你熟悉常规的 SQL 数据,通过下表可以更好的理解 MongoDB 的条件语句查询:

操作格式范例RDBMS中的类似语句
等于 {<key>:<value>} db.col.find({"by":"菜鸟教程"}).pretty() where by = '菜鸟教程'
小于 {<key>:{$lt:<value>}} db.col.find({"likes":{$lt:50}}).pretty() where likes < 50
小于或等于 {<key>:{$lte:<value>}} db.col.find({"likes":{$lte:50}}).pretty() where likes <= 50
大于 {<key>:{$gt:<value>}} db.col.find({"likes":{$gt:50}}).pretty() where likes > 50
大于或等于 {<key>:{$gte:<value>}} db.col.find({"likes":{$gte:50}}).pretty() where likes >= 50
不等于 {<key>:{$ne:<value>}} db.col.find({"likes":{$ne:50}}).pretty() where likes != 50

    

 

 

 

 

 

 

 

 

  1、为admin添加成绩50, 

  > db.user.update({"name":"admin"},{$set:{"grade":50}})
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  2、为aaaaaa添加成绩99:
  > db.user.update({"name":"aaaaaaaaa"},{$set:{"grade":99}})
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

  3、查询grade大于50的:

> db.user.find({"grade":{$gt:50}}).pretty()
  {
    "_id" : ObjectId("5694714a3a18f4867aecbcd2"),
    "name" : "aaaaaaaaa",
    "password" : "123456",
    "grade" : 99
  }

4、查询成绩大于等于50的:
> db.user.find({"grade":{$gte:50}}).pretty()
  {
  "_id" : ObjectId("56946fba3a18f4867aecbcd1"),
  "name" : "admin",
  "password" : "123456",
  "title" : "admin",
  "grade" : 50
  }

  {
    "_id" : ObjectId("5694714a3a18f4867aecbcd2"),
    "name" : "aaaaaaaaa",
    "password" : "123456",
    "grade" : 99
  }

 -----db.xx.find({group:{$nin:["0",""]}}) 

 

 

--- $nin:["0"] 
--- {”Group“:{$ne:”0“}} 
------------------2016-5-26 14:01:50--修改--
    source:【1】mongoDb查询
 

 

posted @ 2016-01-12 11:53  半天的半天  阅读(2636)  评论(0编辑  收藏  举报