教程 > mongodb 教程 > 阅读:21

mongodb 查询文档——迹忆客-ag捕鱼王app官网

在本章中,我们将学习如何从 mongodb 集合中查询文档。

find() 方法

要从 mongodb 集合中查询数据,需要使用 mongodb 的find()方法。

语法

find()方法的基本语法如下

>db.collection_name.find()

find() 方法将以非结构化方式显示所有文档。

示例

假设我们创建了一个名为 mycol 的集合

> use sampledb
switched to db sampledb
> db.createcollection("mycol")
{ "ok" : 1 }
>

并使用 insert() 方法在其中插入 3 个文档,如下所示

> db.mycol.insert([
    {
        title: "mongodb overview",
        description: "mongodb is no sql database",
        by: "jiyik",
        url: "http://www.jiyik.com",
        tags: ["mongodb", "database", "nosql"],
        likes: 100
    },
    {
        title: "nosql database",
        description: "nosql database doesn't have tables",
        by: "jiyik",
        url: "http://www.jiyik.com",
        tags: ["mongodb", "database", "nosql"],
        likes: 20,
        comments: [
            {
                user:"user1",
                message: "my first comment",
                datecreated: new date(2013,11,10,2,35),
                like: 0
            }
        ]
    }
])

以下方法检索集合中的所有文档 -

> db.mycol.find()
{ "_id" : objectid("5dd4e2cc0821d3b44607534c"), "title" : "mongodb overview", "description" : "mongodb is no sql database", "by" : "jiyik", "url" : "http://www.jiyik.com", "tags" : [ "mongodb", "database", "nosql" ], "likes" : 100 }
{ "_id" : objectid("5dd4e2cc0821d3b44607534d"), "title" : "nosql database", "description" : "nosql database doesn't have tables", "by" : "jiyik", "url" : "http://www.jiyik.com", "tags" : [ "mongodb", "database", "nosql" ], "likes" : 20, "comments" : [ { "user" : "user1", "message" : "my first comment", "datecreated" : isodate("2013-12-09t21:05:00z"), "like" : 0 } ] }
>

pretty() 方法

要以格式化的方式显示结果,可以使用 pretty() 方法。

语法

> db.collection_name.find().pretty()

示例

以下示例从名为 mycol 的集合中检索所有文档,并以易于阅读的格式显示这些文档。

> db.mycol.find().pretty()
{
    "_id" : objectid("5dd4e2cc0821d3b44607534c"),
    "title" : "mongodb overview",
    "description" : "mongodb is no sql database",
    "by" : "jiyik",
    "url" : "http://www.jiyik.com",
    "tags" : [
        "mongodb",
        "database",
        "nosql"
    ],
    "likes" : 100
}
{
    "_id" : objectid("5dd4e2cc0821d3b44607534d"),
    "title" : "nosql database",
    "description" : "nosql database doesn't have tables",
    "by" : "jiyik",
    "url" : "http://www.jiyik.com",
    "tags" : [
        "mongodb",
        "database",
        "nosql"
    ],
    "likes" : 20,
    "comments" : [
        {
            "user" : "user1",
            "message" : "my first comment",
            "datecreated" : isodate("2013-12-09t21:05:00z"),
            "like" : 0
        }
    ]
}

findone() 方法

除了 find() 方法,还有findone()方法,它只返回一个文档。

语法

>db.collectionname.findone()

示例

以下示例检索标题为 mongodb overview 的文档。

> db.mycol.findone({title: "mongodb overview"})
{
    "_id" : objectid("5dd6542170fb13eec3963bf0"),
    "title" : "mongodb overview",
    "description" : "mongodb is no sql database",
    "by" : "jiyik",
    "url" : "http://www.jiyik.com",
    "tags" : [
        "mongodb",
        "database",
        "nosql"
    ],
    "likes" : 100
}

rdbms where 子句在 mongodb 中的等价语句

操作 语法 示例 rdbms等价
等于 {:{$eg;}} db.mycol.find({"by":"jiyik"}).pretty() where by = 'jiyik'
小于 {:{$lt:}} db.mycol.find({"likes":{$lt:50}}).pretty() where likes < 50
小于等于 {:{$lte:}} db.mycol.find({"likes":{$lte:50}}).pretty() where likes <= 50
大于 {:{$gt:}} db.mycol.find({"likes":{$gt:50}}).pretty() where likes > 50
大于等于 {:{$gte:}} db.mycol.find({"likes":{$gte:50}}).pretty() where likes >= 50
不相等 {:{$ne:}} db.mycol.find({"likes":{$ne:50}}).pretty() where likes != 50
在数组中 {:{$in:[, ,……]}} db.mycol.find({"name":{$in:["raj", "ram", "raghu"]}}).pretty() where name 匹配 :["raj", "ram", "raghu"] 中的任何值
不在数组中 {:{$nin:}} db.mycol.find({"name":{$nin:["ramu", "raghav"]}}).pretty() where name 不在数组 :["ramu", "raghav"]中 或者根本不存在

mongodb中的 and

根据 and 条件查询文档,需要使用 $and 关键字。

语法

以下是 and 的基本语法

>db.mycol.find({ $and: [ {:}, { :} ] })

示例

以下示例将显示由“jiyik”编写且标题为“mongodb overview”的所有教程。

> db.mycol.find({$and:[{"by":"jiyik"},{"title": "mongodb overview"}]}).pretty()
{
    "_id" : objectid("5dd4e2cc0821d3b44607534c"),
    "title" : "mongodb overview",
    "description" : "mongodb is no sql database",
    "by" : "jiyik",
    "url" : "http://www.jiyik.com",
    "tags" : [
        "mongodb",
        "database",
        "nosql"
    ],
    "likes" : 100
}
>

对于上面给出的示例,等效的 where 子句将是' where by = 'jiyik' and title = 'mongodb overview' '。我们可以在 find 子句中传递任意数量的键值对。

mongodb中的 or

根据 or 条件查询文档,需要使用$or关键字。

语法

以下是or的基本语法

>db.mycol.find(
   {
      $or: [
         {key1: value1}, {key2:value2}
      ]
   }
).pretty()

示例

以下示例将显示由“jiyik”编写或标题为“mongodb overview”的所有教程。

>db.mycol.find({$or:[{"by":"jiyik"},{"title": "mongodb overview"}]}).pretty()
{
   "_id": objectid(7df78ad8902c),
   "title": "mongodb overview", 
   "description": "mongodb is no sql database",
   "by": "jiyik",
   "url": "http://www.jiyik.com",
   "tags": ["mongodb", "database", "nosql"],
   "likes": "100"
}
>

andor 一起使用

以下示例将显示点赞数大于 10 且标题为“mongodb overview”或“jiyik”的文档。等效的 sql where 子句是'where likes>10 and (by = 'jiyik' or title = 'mongodb overview')'

>db.mycol.find({"likes": {$gt:10}, $or: [{"by": "jiyik"},
   {"title": "mongodb overview"}]}).pretty()
{
   "_id": objectid(7df78ad8902c),
   "title": "mongodb overview", 
   "description": "mongodb is no sql database",
   "by": "jiyik",
   "url": "http://www.jiyik.com",
   "tags": ["mongodb", "database", "nosql"],
   "likes": "100"
}
>

mongodb 中的 nor

要根据 nor 条件查询文档,需要使用 $nor 关键字

语法

以下是not的基本语法

>db.collection_name.find(
    {
        $nor: [
            {key1: value1}, {key2:value2}
        ]
    }
)

示例

假设我们在集合empdetails 中插入了 3 个文档,如下所示

db.empdetails.insertmany(
    [
        {
            first_name: "radhika",
            last_name: "sharma",
            age: "26",
            e_mail: "radhika_sharma.123@gmail.com",
            phone: "9000012345"
        },
        {
            first_name: "rachel",
            last_name: "christopher",
            age: "27",
            e_mail: "rachel_christopher.123@gmail.com",
            phone: "9000054321"
        },
        {
            first_name: "fathima",
            last_name: "sheik",
            age: "24",
            e_mail: "fathima_sheik.123@gmail.com",
            phone: "9000054321"
        }
    ]
)

以下示例将检索名字不是“radhika”且姓氏不是“christopher”的文档

> db.empdetails.find(
    {
        $nor:[
            {"first_name": "radhika"},
            {"last_name": "christopher"}
        ]
    }
).pretty()
{
    "_id" : objectid("5dd631f270fb13eec3963bef"),
    "first_name" : "fathima",
    "last_name" : "sheik",
    "age" : "24",
    "e_mail" : "fathima_sheik.123@gmail.com",
    "phone" : "9000054321"
}

mongodb 中的 not

要根据 not 条件查询文档,需要使用 $not 关键字。

语法

以下是not的基本语法

>db.collection_name.find(
    {
        $not: [
            {key1: value1}, {key2:value2}
        ]
    }
).pretty()

示例

以下示例将检索年龄不大于 25 的文档

> db.empdetails.find( { "age": { $not: { $gt: "25" } } } )
{
    "_id" : objectid("5dd6636870fb13eec3963bf7"),
    "first_name" : "fathima",
    "last_name" : "sheik",
    "age" : "24",
    "e_mail" : "fathima_sheik.123@gmail.com",
    "phone" : "9000054321"
}

查看笔记

扫码一下
查看教程更方便
网站地图