MongoDB笔记(四) 查询操作(详细)

查询操作(详细)

        1.查询集合所有文档

2.等值条件查询

3.嵌套文档查询

4.数组查询

5.查找null值字段,查找指定无值字段

6.或条件查找

7.查询操作符

8.正则表达式

9.区间查询


1.查询集合所有文档

db.goodstest.find()         //goodstest:collection name
db.goodstest.find().pretty()
> db.goodstest.find()
{ "_id" : ObjectId("60744b754a2bf7c4568674b5"), "name" : "《C语言》", "price" : 33.2, "adddate" : "2020-10-1", "allow" : true, "baseinf" : { "ISBN" : 1838388, "press" : "清华大学出版社" }, "tags" : [ "good", "book", "it", "program" ] }
{ "_id" : ObjectId("60744d964a2bf7c4568674b8"), "name" : "《c program》", "price" : 45 }
{ "_id" : 2222, "toy" : null }
{ "_id" : 1112 }
> db.goodstest.find().pretty()
{
        "_id" : ObjectId("60744b754a2bf7c4568674b5"),
        "name" : "《C语言》",
        "price" : 33.2,
        "adddate" : "2020-10-1",
        "allow" : true,
        "baseinf" : {
                "ISBN" : 1838388,
                "press" : "清华大学出版社"
        },
        "tags" : [
                "good",
                "book",
                "it",
                "program"
        ]
}
{
        "_id" : ObjectId("60744d964a2bf7c4568674b8"),
        "name" : "《c program》",
        "price" : 45
}
{ "_id" : 2222, "toy" : null }
{ "_id" : 1112 }

2.等值条件查询

> db.goodstest.find({name:"《c program》"})
{ "_id" : ObjectId("60744d964a2bf7c4568674b8"), "name" : "《c program》", "price" : 45 }
> db.goodstest.find(
... {name:"《c program》"},
... {name:1,price:1,_id:0}         //1表示显示,0表示不显示
... )
{ "name" : "《c program》", "price" : 45 }

3.嵌套文档查询

> db.goodstest.find({"baseinf.press":"清华大学出版社"})
{ "_id" : ObjectId("60744b754a2bf7c4568674b5"), "name" : "《C语言》", "price" : 33.2, "adddate" : "2020-10-1", "allow" : true, "baseinf" : { "ISBN" : 1838388, "press" : "清华大学出版社" }, "tags" : [ "good", "book", "it", "program" ] }
> db.goodstest.find({baseinf.press:"清华大学出版社"})
2021-04-12T22:38:00.826+0800 E QUERY    [thread1] SyntaxError: missing : after property id @(shell):1:26                 //注意baseinf.press要加双引号

4.数组查询

> db.goodstest.find({tags:["good","book","it","program"]}).pretty()
> db.goodstest.find({tags:"good"}).pretty()
> db.goodstest.find({tags:{$size:4}}).pretty()
> db.goodstest.find({tags:["good","book","it","program"]}).pretty()
{
        "_id" : ObjectId("60744b754a2bf7c4568674b5"),
        "name" : "《C语言》",
        "price" : 33.2,
        "adddate" : "2020-10-1",
        "allow" : true,
        "baseinf" : {
                "ISBN" : 1838388,
                "press" : "清华大学出版社"
        },
        "tags" : [
                "good",
                "book",
                "it",
                "program"
        ]
}
> db.goodstest.find({tags:"good"}).pretty()
{
        "_id" : ObjectId("60744b754a2bf7c4568674b5"),
        "name" : "《C语言》",
        "price" : 33.2,
        "adddate" : "2020-10-1",
        "allow" : true,
        "baseinf" : {
                "ISBN" : 1838388,
                "press" : "清华大学出版社"
        },
        "tags" : [
                "good",
                "book",
                "it",
                "program"
        ]
}
> db.goodstest.find({tags:{$size:4}}).pretty()
{
        "_id" : ObjectId("60744b754a2bf7c4568674b5"),
        "name" : "《C语言》",
        "price" : 33.2,
        "adddate" : "2020-10-1",
        "allow" : true,
        "baseinf" : {
                "ISBN" : 1838388,
                "press" : "清华大学出版社"
        },
        "tags" : [
                "good",
                "book",
                "it",
                "program"
        ]
}

5.查找null值字段,查找指定无值字段

> db.goodstest.find({_id:2222,toy:null})
{ "_id" : 2222, "toy" : null }
> db.goodstest.find({_id:1112,toy:{$exists:false}})
{ "_id" : 1112 }

6.或条件查找

> db.goodstest.find({price:{$in:[33,33.2]}}).pretty()
{
        "_id" : ObjectId("60744b754a2bf7c4568674b5"),
        "name" : "《C语言》",
        "price" : 33.2,
        "adddate" : "2020-10-1",
        "allow" : true,
        "baseinf" : {
                "ISBN" : 1838388,
                "press" : "清华大学出版社"
        },
        "tags" : [
                "good",
                "book",
                "it",
                "program"
        ]
}

7.查询操作符

MongoDB笔记(四) 查询操作(详细)_第1张图片

小于和小于等于(<和<=):

> db.goodstest.find({price:{$lt:45}}).pretty()
{
        "_id" : ObjectId("60744b754a2bf7c4568674b5"),
        "name" : "《C语言》",
        "price" : 33.2,
        "adddate" : "2020-10-1",
        "allow" : true,
        "baseinf" : {
                "ISBN" : 1838388,
                "press" : "清华大学出版社"
        },
        "tags" : [
                "good",
                "book",
                "it",
                "program"
        ]
}
> db.goodstest.find({price:{$lte:45}}).pretty()
{
        "_id" : ObjectId("60744b754a2bf7c4568674b5"),
        "name" : "《C语言》",
        "price" : 33.2,
        "adddate" : "2020-10-1",
        "allow" : true,
        "baseinf" : {
                "ISBN" : 1838388,
                "press" : "清华大学出版社"
        },
        "tags" : [
                "good",
                "book",
                "it",
                "program"
        ]
}
{
        "_id" : ObjectId("60744d964a2bf7c4568674b8"),
        "name" : "《c program》",
        "price" : 45
}

大于和大于等于(>和>=):

> db.goodstest.find({price:{$gt:33.2}}).pretty()
{
        "_id" : ObjectId("60744d964a2bf7c4568674b8"),
        "name" : "《c program》",
        "price" : 45
}
> db.goodstest.find({price:{$gte:33.2}}).pretty()
{
        "_id" : ObjectId("60744b754a2bf7c4568674b5"),
        "name" : "《C语言》",
        "price" : 33.2,
        "adddate" : "2020-10-1",
        "allow" : true,
        "baseinf" : {
                "ISBN" : 1838388,
                "press" : "清华大学出版社"
        },
        "tags" : [
                "good",
                "book",
                "it",
                "program"
        ]
}
{
        "_id" : ObjectId("60744d964a2bf7c4568674b8"),
        "name" : "《c program》",
        "price" : 45
}

不等于(!=):

> db.goodstest.find({price:{$ne:33.2}}).pretty()
{
        "_id" : ObjectId("60744d964a2bf7c4568674b8"),
        "name" : "《c program》",
        "price" : 45
}
{ "_id" : 2222, "toy" : null }
{ "_id" : 1112 }

与:

> db.goodstest.find({price:{$ne:33.2},name:"《c program》"}).pretty()
{
        "_id" : ObjectId("60744d964a2bf7c4568674b8"),
        "name" : "《c program》",
        "price" : 45
}

或:

> db.goodstest.find({$or:[{price:45},{_id:2222}]}).pretty()
{
        "_id" : ObjectId("60744d964a2bf7c4568674b8"),
        "name" : "《c program》",
        "price" : 45
}
{ "_id" : 2222, "toy" : null }

8.正则表达式

:{$regex:/patten/

patten:

/^查询的值/  :查询值的前面部分固定  相当于“查询的值%”

/查询的值$/ :查询值的后面部分固定  相当于“%查询的值”

/查询的值/ :查询所有包含查询的值的文档  相当于“%查询的值%”

option:

i :不区分大小写字母

> db.goodstest.find().pretty()
{
        "_id" : ObjectId("60744b754a2bf7c4568674b5"),
        "name" : "《C语言》",
        "price" : 33.2,
        "adddate" : "2020-10-1",
        "allow" : true,
        "baseinf" : {
                "ISBN" : 1838388,
                "press" : "清华大学出版社"
        },
        "tags" : [
                "good",
                "book",
                "it",
                "program"
        ]
}
{
        "_id" : ObjectId("60744d964a2bf7c4568674b8"),
        "name" : "《c program》",
        "price" : 45
}
{
        "_id" : ObjectId("6079579e0f65fbc098679fa3"),
        "name" : "《c语言》",
        "price" : 28
}
> db.goodstest.find({name:{$regex:/^《c/}}).pretty()
{
        "_id" : ObjectId("60744d964a2bf7c4568674b8"),
        "name" : "《c program》",
        "price" : 45
}
{
        "_id" : ObjectId("6079579e0f65fbc098679fa3"),
        "name" : "《c语言》",
        "price" : 28
}
> db.goodstest.find({name:{$regex:/语言》$/}}).pretty()
{
        "_id" : ObjectId("60744b754a2bf7c4568674b5"),
        "name" : "《C语言》",
        "price" : 33.2,
        "adddate" : "2020-10-1",
        "allow" : true,
        "baseinf" : {
                "ISBN" : 1838388,
                "press" : "清华大学出版社"
        },
        "tags" : [
                "good",
                "book",
                "it",
                "program"
        ]
}
{
        "_id" : ObjectId("6079579e0f65fbc098679fa3"),
        "name" : "《c语言》",
        "price" : 28
}

加入option参数和不加option参数的区别:

> db.goodstest.find({name:{$regex:/C语/}}).pretty()
{
        "_id" : ObjectId("60744b754a2bf7c4568674b5"),
        "name" : "《C语言》",
        "price" : 33.2,
        "adddate" : "2020-10-1",
        "allow" : true,
        "baseinf" : {
                "ISBN" : 1838388,
                "press" : "清华大学出版社"
        },
        "tags" : [
                "good",
                "book",
                "it",
                "program"
        ]
}
> db.goodstest.find({name:{$regex:/C语/i}}).pretty()
{
        "_id" : ObjectId("60744b754a2bf7c4568674b5"),
        "name" : "《C语言》",
        "price" : 33.2,
        "adddate" : "2020-10-1",
        "allow" : true,
        "baseinf" : {
                "ISBN" : 1838388,
                "press" : "清华大学出版社"
        },
        "tags" : [
                "good",
                "book",
                "it",
                "program"
        ]
}
{
        "_id" : ObjectId("6079579e0f65fbc098679fa3"),
        "name" : "《c语言》",
        "price" : 28
}

9.区间查询

> db.goodstest.find({price:{$gt:30,$lt:40}}).pretty()
{
        "_id" : ObjectId("60744b754a2bf7c4568674b5"),
        "name" : "《C语言》",
        "price" : 33.2,
        "adddate" : "2020-10-1",
        "allow" : true,
        "baseinf" : {
                "ISBN" : 1838388,
                "press" : "清华大学出版社"
        },
        "tags" : [
                "good",
                "book",
                "it",
                "program"
        ]
}

10.limit和skip方法

limit()函数用于限制查询结果的个数,若匹配不到括号里数量的结果,则返回匹配数量的结果

> db.goodstest.find().limit(2).pretty()
{
        "_id" : ObjectId("60744b754a2bf7c4568674b5"),
        "name" : "《C语言》",
        "price" : 33.2,
        "adddate" : "2020-10-1",
        "allow" : true,
        "baseinf" : {
                "ISBN" : 1838388,
                "press" : "清华大学出版社"
        },
        "tags" : [
                "good",
                "book",
                "it",
                "program"
        ]
}
{
        "_id" : ObjectId("60744d964a2bf7c4568674b8"),
        "name" : "《c program》",
        "price" : 45
}

skip()函数用于略过指定个数的文档

> db.goodstest.find().skip(2).pretty()
{
        "_id" : ObjectId("6079579e0f65fbc098679fa3"),
        "name" : "《c语言》",
        "price" : 28
}

下一篇:MongoDB笔记(五) 更新操作(详细)

你可能感兴趣的:(MongoDB,mongodb,sql)