mongodb基本查询语法

1. mongodb使用,请先安装Robo 3T工具,安装地址:
https://robomongo.org/

2. 注意:执行语句按F5

mongodb相关表

表名描述
work_grid_sys工作网格
area_grid_sys单元网格
wh_sys部件普查信息

1.查询语句

//根据_id查询
db.getCollection('fs.files').find({
        "_id":ObjectId("5cd24889193c3d0001230d93")
    })

//根据类型、关键字查询
db.getCollection('wh_sys').find({
    "properties.type":"WH_0201",
    "properties.objcode":"4117280201000001"})

2.聚合查询,相当于mysql的group by

//根据properties.zrcode查询总数
db.work_grid_sys.aggregate([{$group: {_id: '$properties.zrcode', personCount: {$sum: 1}}}])

//根据properties.zrcode查询详情
db.getCollection('work_grid_sys').find({
    "properties.zrcode":"411728001Z013"})

// 分组查询相同文件名数据和大小
db.getCollection('fs.files').aggregate( [
                         { $group: { _id: "$filename", num_tutorial : { $sum: 1 }, sum_length : { $sum: "$length" } } },
                         {$match:{num_tutorial:{$gt:5}}}
                       ], 
											 {$sort:{sum_length:-1}}
											 );
超过16M就需要加,允许缓存 { allowDiskUse: true }
db.getCollection('fs.files').aggregate( [
                         { $group: { _id: "$filename", num_tutorial : { $sum: 1 }, sum_length : { $sum: "$length" } } },
                         {$match:{num_tutorial:{$gt:5}}}
                       ], { allowDiskUse: true } ,
											 {$sort:{sum_length:-1}}
											 );

3.查询某集合指定字段

db.getCollection("area_grid_sys").find({}, 
    { 
        "properties.gr_code" : 1.0, 
        "properties.gr_name" : 1.0, 
        "properties.gr_up_code" : 1.0, 
        "properties.gr_up_name" : 1.0, 
        "_id" : 0.0
    }
);

在这里插入图片描述

4.模糊查询

db.getCollection('work_grid_sys').find({"properties.zrcode":{$regex:/411728/}})

5.关联表查询

// Requires official MongoShell 3.6+
use sums_440300_basic_files_v20;
db.getCollection("fs.chunks").aggregate(
    [
        { 
            "$lookup" : {
                "from" : "fs.files", 
                "localField" : "files_id", 
                "foreignField" : "_id", 
                "as" : "files"
            }
        }
    ], 
    { 
        "allowDiskUse" : false
    }
);

在这里插入图片描述

6.坐标点查询

https://tykj.cszhx.top:30720/api/gridApi/gridSys/getGridByCondition
Content-Type:application/json
{
  "coordinates": [
    112.87921358056643,28.197614840820314
  ],
  "type": "Point"
}

在这里插入图片描述

聚合的表达式

表达式描述实例
$sum计算总和。db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "by_user", num_tutorial : { s u m : " sum : " sum:"likes"}}}])
$avg计算平均值db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "by_user", num_tutorial : { a v g : " avg : " avg:"likes"}}}])
$min获取集合中所有文档对应值得最小值。db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "by_user", num_tutorial : { m i n : " min : " min:"likes"}}}])
$max获取集合中所有文档对应值得最大值。db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "by_user", num_tutorial : { m a x : " max : " max:"likes"}}}])
$push在结果文档中插入值到一个数组中。db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "by_user", url : { p u s h : " push: " push:"url"}}}])
$addToSet在结果文档中插入值到一个数组中,但不创建副本。db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "by_user", url : { a d d T o S e t : " addToSet : " addToSet:"url"}}}])
$first根据资源文档的排序获取第一个文档数据。db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "by_user", first_url : { f i r s t : " first : " first:"url"}}}])
$last根据资源文档的排序获取最后一个文档数据db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "by_user", last_url : { l a s t : " last : " last:"url"}}}])
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值