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 : {sum:"sum : "sum:"likes"}}}]) |
| $avg | 计算平均值 | db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "by_user", num_tutorial : {avg:"avg : "avg:"likes"}}}]) |
| $min | 获取集合中所有文档对应值得最小值。 | db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "by_user", num_tutorial : {min:"min : "min:"likes"}}}]) |
| $max | 获取集合中所有文档对应值得最大值。 | db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "by_user", num_tutorial : {max:"max : "max:"likes"}}}]) |
| $push | 在结果文档中插入值到一个数组中。 | db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "by_user", url : {push:"push: "push:"url"}}}]) |
| $addToSet | 在结果文档中插入值到一个数组中,但不创建副本。 | db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "by_user", url : {addToSet:"addToSet : "addToSet:"url"}}}]) |
| $first | 根据资源文档的排序获取第一个文档数据。 | db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "by_user", first_url : {first:"first : "first:"url"}}}]) |
| $last | 根据资源文档的排序获取最后一个文档数据 | db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "by_user", last_url : {last:"last : "last:"url"}}}]) |