mongo教程二、高级查询

1、插入mongo 数据库

    

    基础数据:

db.books.insertMany([
{"name":"mongodb 入门到精通","price":45,"author":["oceangray","sky"],"publish":[{"pubName":"机械出版社","地址":"北京市海淀区XXX","contact":"010-82707199"}],pubDate:"2016-01-01"},
{"name":"java 实战开发","price":28,"author":["lili"],"publish":[{"pubName":"北京邮电出版社","地址":"北京市海淀区XXX","contact":"010-82707199"}],pubDate:"2017-03-01"},
{"name":"mysql 性能调优","price":33,"author":["alex"],"publish":[{"pubName":"清华出版社","地址":"北京市海淀区XXX","contact":"010-82707199"}],pubDate:"2016-04-01"},
{"name":"hadoop 实战开发","price":69,"author":["kenny"],"publish":[{"pubName":"机械出版社","地址":"北京市海淀区XXX","contact":"010-82707199"}],pubDate:"2016-07-01"},
{"name":"tensorflow 详解","price":81,"author":["nick"],"publish":[{"pubName":"北京师范大学出版社","地址":"北京市海淀区XXX","contact":"010-82707199"}],pubDate:"2017-03-01"},
{"name":"spring boots 实战","price":27,"author":["oceangray","nick"],"publish":[{"pubName":"北京邮电出版社","地址":"北京市海淀区XXX","contact":"010-82707199"}],pubDate:"2016-01-01"},
{"name":"go语言开发","price":44,"author":["oceangray"],"publish":[{"pubName":"机械出版社","地址":"北京市海淀区XXX","contact":"010-82707199"}],pubDate:"2017-09-01"},
{"name":"solr cloud 经典教材","price":36,"author":["amy"],"publish":[{"pubName":"北京邮电出版社","地址":"北京市海淀区XXX","contact":"010-82707199"}],pubDate:"2018-01-01"},
{"name":"dubbo 事务处理详解","price":25,"author":["alice"],"publish":[{"pubName":"机械出版社","地址":"北京市海淀区XXX","contact":"010-82707199"}],pubDate:"2018-02-01"},
{"name":"python 装饰器","price":16,"author":["leo"],"publish":[{"pubName":"机械出版社","地址":"北京市海淀区XXX","contact":"010-82707199"}],pubDate:"2017-04-01"},
{"name":"django 开发宝典","price":19,"author":["sky"],"publish":[{"pubName":"北京师范大学出版社","地址":"北京市海淀区XXX","contact":"010-82707199"}],pubDate:"2017-05-01"},
{"name":"netty 实战宝典","price":32,"author":["sky"],"publish":[{"pubName":"机械出版社","地址":"北京市海淀区XXX","contact":"010-82707199"}],pubDate:"2017-04-01"}
])


2、查询:

    2.1、查询总数:db.books.count()

    2.2、查询书名为“”go语言开发“”

        db.books.find({"name":"go语言开发"})

        image.png

        指定的字段:

        db.books.find({"name":"go语言开发"},{"name":1})

        db.books.find({"name":"go语言开发"},{"name":1,"_id":0}))

     2.3、正则查询:

            查询书名包含开发的书籍

        db.books.find({"name":/开发/},{"name":1})

        image.png

        或者:

         db.books.find({"name":{$regex:"开发"}},{"name":1})

        image.png

        开头匹配:

         db.books.find({"name":{"$regex":"^java"}},{"name":1})

        db.books.find({"name":/^java/},{"name":1})     )}))))

        image.png

        结尾匹配:

         db.books.find({"name":{"$regex":"开发$"}},{"name":1})

        db.books.find({"name":/开发$/},{"name":1})     )}))))

        image.png

2、4 比较运算符

    等于:$eq

    db.books.find({"name":{$eq:"java 实战开发"}})

      image.png

    小于:$lt:

        db.books.find({"price":{$lt:30}},{"name":1,"price":1}).pretty()

        image.png

        大于:$gt:

          db.books.find({"price":{$gt:30}},{"name":1,"price":1}).pretty()

        小于等于:$lte:

         db.books.find({"price":{$lte:30}},{"name":1,"price":1}).pretty()

        大于等于:$gte:

         db.books.find({"price":{$gte:30}},{"name":1,"price":1}).pretty()

        不等于:

        db.books.find({"price":{$ne:30}},{"name":1,"price":1}).pretty()

        包含:

        db.books.find({"author":{$in:["oceangray","sky"]}},{name:1,author:1})

        image.png

        不包含:

        db.books.find({"author":{$nin:["oceangray","sky"]}},{"name":1,"author":1})

            image.png

    2.5 逻辑查询操作符:

        2.5.1:$or 查询:

            db.books.find({$or:[{"publish.pubName":"机械出版社"},{"price":{$lt:40}}]}).pretty()

            image.png

    2.5.2 $nor 查询:

         db.books.find({$nor:[{"publish.pubName":"机械出版社"},{"price":{$lt:40}}]}).pretty()

        image.png

        2.5.3 $and:

         db.books.find({$and:[{name:"oceangray"},"price":{"gt":30}]})

        image.png

        2.5.4 $not

        image.png


       2.6元查询操作符:

        2.6.1$exits:

            db.books.find({"address":{$exists:true}})

            image.png

        2.6.2 $type

                 

                            

类型数字备注
Double1
String2
Object3
Array4
Binary data5
Undefined6已废弃。
Object id7
Boolean8
Date9
Null10
Regular Expression11
JavaScript13
Symbol14
JavaScript (with scope)15
32-bit integer16
Timestamp17
64-bit integer18
Min key255Query with -1.
Max key127

db.books.find({"name":{$type:2}})
    image.png


2.7 评价查询操作符:

        2.7.1 :$regex

        db.books.find({"name":{$regex:"mongo"}}).pretty()

        2.7.2 :$mod 

        db.books.find({"price":{$mod:[10,4]}})

        image.png

        2.7.3: $text 是对文本索引的字段内容进行文本搜索

        创建索引:

        db.books.createIndex(name:"text")

        image.png