• mongodb分享(二)


     
     

    上次讲的:查询findfindonepretty、条件操作符 (大于、小于、大于等于、小于等于、不等于,$type)、limitskip、sort、Db.postjson.getIndexes();

    $type$type操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果。

    BSON类型:   一种增强型的JSON格式,比JSON数据类型多,是Binary JSON 的简称。特点:解释更快。

    https://blog.csdn.net/leshami/article/details/52668870

    https://www.runoob.com/mongodb/mongodb-operators-type.html

    上次遗留问题: mongodb与mysql 的区别?为什么用mongdb而不用mysql?

     1)mongodb支持自动分片,方便帮集群添加和删除节点。对应用程序而言,好像始终在使用一个单机有mongodb服务器一样。它会自动处理数据在数据在分片上的分布,而mysql不行,mysql在增加集群节点时,需要人工分表,做数据迁移。

    2 ) mongodb可以随意扩展字段,需要增加字段时很轻松就加进去了,而mysql增加字段还需要改表结果,所以mongodb适合存储这种非结构化的信息。

     

     

    聚合:aggregate    管道聚合:pipeline aggregate

    1 。group by

    db.postjson.aggregate([{$group:{_id:"$bwHotelID",reviewcnt:{$sum:1}}}]);

    这里的$sum:1  相当于count(*)    要对某个字段进行sum 应写成 $sum : “$字段名

     

    2。 group by + where

    db.postjson.aggregate([{$group:{_id:"$bwHotelID",reviewcnt:{$sum:1}}},{$match:{bwHotelID:284500}}]);

    db.postjson.aggregate([{$group:{_id:"$bwHotelID",reviewcnt:{$sum:1}}},{$match:{_id:160733}}]);

    db.postjson.aggregate([{$match:{bwHotelID:160733}}]);

    这里的$match就相当于sql语法中的where

     

    3。group by + 字段筛选

    db.postjson.aggregate([{$group:{_id:"$bwHotelID",reviewcnt:{$sum:1}}},{$project:{_id:0,reviewcnt:1}}]);

    db.postjson.aggregate([{$group:{_id:"$bwHotelID",reviewcnt:{$sum:1}}},{$project:{_id:0,reviewcnt:1}},{$match:{reviewcnt:4}}]);

    db.postjson.aggregate([{$group:{_id:"$bwHotelID",reviewcnt:{$sum:1}}},{$project:{_id:0,reviewcnt:1}},{$match:{reviewcnt:{$ne:4}}}]);   

    条件操作符在match中仍然可以用。 $gt  $lt  $gte  $lte  $ne  $in

     

     

    4 group by + where + 字段筛选 + having 

     

     

    1. sum
    2. avge
    3. min
    4. max
    5. push 在结果文档中插入值至一个数组 ???
    6. addToSet   ????
    7. first
    8. last

     

    管道聚合:   语法:db.collection.aggregate(pipeline, options)

    1) project  字段筛选 及字段重命名。

    2) match     where

    3) limit       limit

    4) skip       skip

    5) unwind

    6) group      group by     $group语法: { $group: { _id: <expression>, <field1>: { <accumulator1> : <expression1> }, ... } }

    7) sort       order by

    8) geoNear

     

     

    https://blog.csdn.net/congcong68/article/details/51619882

    https://blog.csdn.net/congcong68/article/details/51620040

    注:group: 如果_idnull  相当于select  count(*) from table

    mongodb权威指南.pdf   p129

     7.1 聚合框架:

    聚合框架中的操作符:

    1)$project

    2) $group

    3) $sort

    4) $limit

    7.2 管道操作符

    1)$match

    2)$project   (提取字段、重命名字段、对字段进行操作)

                管道表达式:

                数学表达式: $add、$subtract   $multiply   $divide   $mod

                日期表达式: $month、$week、$dayOfMonth、$dayOfWeek、$dayOfYear、$hour、$minute、$second

                字符串表达式:$substr、$concat、$toLower、$toUpper

        逻辑表达式:$cmp、$strcasecmp、$gt、$lt、$gte、$lte、$ne、$eq、$in、$nin、$and、$or、$not、$cond、$ifNull

    3) $group

                group by a,b ====={"$group":{"_id":{a:$a,b:$b}}}    

                   分组操作符:

         算术操作符:$sum、$avg  

        极值操作符:  $max、$min、$first、$last

         数组操作符:  $addToSet、$push

        

    4)$unwind

    5) $sort

    6) $limit

    7) skip

    8) 使用管道 :

     

     ============mongodb某个字段模糊匹配中文=============

    {'source_hotel_id':'4056562','room_name':{'$regex':'钟'}} 匹配钟点房。
    {"hotelname":{'$regex':'派酒店廊坊香河家'}}
    
    
    第一种:使用 $regex 第二种:使用/钟/ 两种均可,个人觉得还是加/敏/的这种更简单。
    1. >db.posts.find({post_text:{$regex:"shouce.ren"}}) {'source_hotel_id':'4056562','room_name':{$regex:'钟'}}
    2. >db.posts.find({post_text:/shouce.ren/}) {'source_hotel_id':'4056562','room_name':/钟/}
    3. 查手册《mongodb参考手册-新》
    
    
    =====查询价格计划名称中包含“小时”两个字,但不包含“钟点房”三个字的正则。============
    {$and:[{"room_name":{$regex:"[^钟点房]"}},{"room_name":{$regex:"小时"}}]}
    {$and:[{"room_name":{$regex:"[^钟点房]"}},{"room_name":/小时/}]}
    上面两个用正则非^单独用可以,但加上and就不管用了,最后还是用了下面的not联合正则使用管用了。
    {$and:[{"room_name":{$not:{$regex:"钟点房"}}},{"room_name":/小时/}]}
     
     
    =============20210408==============
    mongodb聚合查询:
    非常好的参考链接:
    https://www.cnblogs.com/zhoujie/p/mongo1.html#top
     
    db.aggregation.aggregate([{$group : {_id : "$dev", num_tutorial : {$sum : 1}}}]);
     
    db.aggregation.aggregate( [ { $unwind: "$samples_Eptp" }, { $group: { _id: "$samples_Eptp.v", qty: { $sum:1} } } ] )
     
     
    db.aggregation.aggregate( [ {$match:{"dev":"352(216)","sn":"3006096460","day":"2021-03-11"}}, { $unwind: "$samples_Eptp" }, { $group: { _id: "$samples_Eptp.v", qty: { $sum:1} } } ] )
     

     mongodb中的$unwind管道聚合

    需求:

    {
        "_id" : ObjectId("5951c5de567ebff0d5011fba"),
        "name" : "陈晓婵",
        "address" : "北京朝阳区",
        "weekday" : [ 
            1, 
            2, 
            3, 
            4, 
            5
        ]
    }

    对weekday进行拆分:

    db.getCollection('chenxiaochantest').aggregate(
     [
      {
         $unwind:"$weekday"
      }
     ]
    )

    拆分结果:

    /* 1 */
    {
        "_id" : ObjectId("5951c5de567ebff0d5011fba"),
        "name" : "陈晓婵",
        "address" : "北京朝阳区",
        "weekday" : 1
    }
     
    /* 2 */
    {
        "_id" : ObjectId("5951c5de567ebff0d5011fba"),
        "name" : "陈晓婵",
        "address" : "北京朝阳区",
        "weekday" : 2
    }
     
    /* 3 */
    {
        "_id" : ObjectId("5951c5de567ebff0d5011fba"),
        "name" : "陈晓婵",
        "address" : "北京朝阳区",
        "weekday" : 3
    }
     
    /* 4 */
    {
        "_id" : ObjectId("5951c5de567ebff0d5011fba"),
        "name" : "陈晓婵",
        "address" : "北京朝阳区",
        "weekday" : 4
    }
     
    /* 5 */
    {
        "_id" : ObjectId("5951c5de567ebff0d5011fba"),
        "name" : "陈晓婵",
        "address" : "北京朝阳区",
        "weekday" : 5
    }

    使用$unwind可以将weekday中的每个数据都被分解成一个文档,并且除了weekday的值不同外,其他的值都是相同的.

    实例讲解:

    {
        "_id" : ObjectId("5951ca15567ebff0d5011fbb"),
        "name" : "陈晓婵",
        "address" : "北京朝阳",
        "lunch" : [ 
            {
                "food" : "baozi",
                "fruit" : "taozi"
            }, 
            {
                "food" : "miaotiao",
                "fruit" : "xigua"
            }
        ]
    }

    对lunch进行拆分:

    db.getCollection('chenxiaochantest2').aggregate(
      [
        {
           $unwind:"$lunch"
        }
      ]
    )

    拆封结果:

    /* 1 */
    {
        "_id" : ObjectId("5951ca15567ebff0d5011fbb"),
        "name" : "陈晓婵",
        "address" : "北京朝阳",
        "lunch" : {
            "food" : "baozi",
            "fruit" : "taozi"
        }
    }
     
    /* 2 */
    {
        "_id" : ObjectId("5951ca15567ebff0d5011fbb"),
        "name" : "陈晓婵",
        "address" : "北京朝阳",
        "lunch" : {
            "food" : "miaotiao",
            "fruit" : "xigua"
        }
    }

    转载来源:https://blog.csdn.net/chenxiaochan/article/details/73771879

     

     

     

     

     

  • 相关阅读:
    [Javascript]发布一个自己写的日期控件:DateTimeList
    Oracle PL/SQL 编程手册(SQL大全)
    [乱七八糟][转]程序版吉祥三宝
    [乱七八糟][转]这不是你想象中的软件产业
    [随文杂记]生男好还是生女好?
    [SqlServer]链接数据库存储过程
    [音乐天堂]辛德勒名单原声大碟
    [C#]WinFrom中的DataGrid单击选择行
    [乱七八糟]《进化论——人类科学史上最大的谎言》
    [乱七八糟]《阿甘正传》点评
  • 原文地址:https://www.cnblogs.com/yoyoma0355/p/9212409.html
Copyright © 2020-2023  润新知