III.Data Modeling and Aggregation(数据模型和整合)
Recipe 3-1. Data Model Using an Embedded Document(数据模型使用整合文档)
1.一对一的关系
比如:
{
_id: "James",
name: "James William"
}
{
student_id: "James",
street: "123 Hill Street",
city: "New York",
state: "US",
}
整合:
{
_id: "James",
name: "James William",
address: {
street: "123 Hill Street",
city: "New York",
state: "US",
}
}
2.一对多的关系
比如:
{
_id: "James",
name: "James William"
}
{
student_id: "James",
street: "123 Hill Street",
city: "New York",
state: "US",
}
{
student_id: "James",
street: "234 Thomas Street",
city: "New Jersey",
state: "US",
}
整合:
{
_id: "James",
name: "James William",
address: [{
street: "123 Hill Street",
city: "New York",
state: "US",
},
{
street: "234 Thomas Street",
city: "New Jersey",
state: "US",
}]
}
Recipe 3-2. Model Tree Structure with Parent References (模型树--Parent)
1.插入如下数据
db.author.insert( { _id: "Practical Apache Spark", parent:"Books" } )
db.author.insert( { _id: "MongoDB Recipes", parent: "Books" } )
db.author.insert( { _id: "Books", parent: "Subhashini" } )
db.author.insert( { _id: "A Framework For Extracting Information From Web Using VTD-XML ' s XPath", parent:"Article" } )
db.author.insert( { _id: "Article", parent: "Subhashini" } )
db.author.insert( { _id: "Subhashini", parent: null } )
2.树结构
Subhashii
Books Article
Practical Apache Spark A Framework For Extracting Information From Web Using VTD-XML
3.查询父级
db.author.findOne( { _id: "MongoDB Recipes" } ).parent
db.author.find( { parent: "Subhashini" } )
Recipe 3-3. Tree Structure with Child References (模型树--Child)
1.插入数据
db.author.insert( { _id: "Practical Apache Spark", children: [] } )
db.author.insert( { _id: "MongoDB", children: [] } )
db.author.insert( { _id: "Books", children: [ "Practical Apache Spark", "MongoDB Recipes" ] } )
db.author.insert( { _id: " A Framework For Extracting Information From Web Using VTD-XML ' s XPath ", children: [] } )
db.author.insert( { _id: "Article", children: [ " A Framework For Extracting Information From Web Using VTD-XML ' s XPath " ] } )
db.categories.insert( { _id: "Subhashini", children: ["Books","Article" ] } )
2.查询子级
db.author.findOne( { _id: "Books" } ).children
db.author.find( { children: "MongoDB Recipes" } )
Recipe 3-4. Tree Structure with an Array of Ancestors (模型树--数组Ancestors)
1.插入数据
db.author.insert( { _id: "Practical Apache Spark", ancestors: ["Subhashini", "Books" ], parent: "Books" } )
db.author.insert( { _id: "MongoDB Recipes", ancestors: ["Subhashini", "Books" ], parent: "Books" } )
db.author.insert( { _id: "Books", ancestors: [ "Subhashini" ],parent: "Subhashini" } )
db.author.insert( { _id: " A Framework For Extracting Information From Web Using VTD-XML ", ancestors: ["Subhashini", "Article" ], parent: "Article" } )
db.author.insert( { _id: "Article", ancestors: [ "Subhashini"], parent: "Subhashini" } )
db.author.insert( { _id: "Subhashini", ancestors: [ ], parent:null } )
2.查询ancestors
db.author.findOne( { _id: "MongoDB Recipes" } ).ancestors
db.author.find( { ancestors: "Subhashini" } )
Recipe 3-5. Aggregation Pipeline(整合渠道)
准备数据
db.orders.insertMany([
{custID:"10001",amount:500,status:"A"},
{custID:"10001",amount:250,status:"A"},
{custID:"10002",amount:200,status:"A"},
{custID:"10001",amount: 300, status:"D"}]);
1.仅仅需要custID
db.orders.aggregate( [ { $project : { custID : 1 , _id : 0 }} ] )
2.查询相当于group的语句,并查询总数
db.orders.aggregate({$group:{_id:"$custID",TotalAmount:{$sum:"$amount"}}});
3.使用$match来求总数
db.orders.aggregate({$match:{status:"A"}},{$group:{_id:"$custID",TotalAmount:{ $sum:"$amount"}}});
4.求平均数
db.orders.aggregate({$group:{_id:"$custID",AverageAmount:{$avg:"$amount"}}});
Recipe 3-6. Map-Reduce
准备数据
db.orders.insertMany([
{custID:"10001",amount:500,status:"A"},
{custID:"10001",amount:250,status:"A"},
{custID:"10002",amount:200,status:"A"},
{custID:"10001",amount: 300, status:"D"}]);
1.创建map的函数--过滤数据查询
var map = function()
{
emit (this.custID, this.amount);
}
2.创建Reduce函数--计算总数
var reduce = function(key, values)
{
return Array.sum(values) ;
}
3.使用上面创建的函数
db.orders.mapReduce(map, reduce,
{
out: "order_totals",query:{status:"A"}
});
4.查询
db.order_totals.find()
5.结果
{ "_id" : "10001", "value" : 750 }
{ "_id" : "10002", "value" : 200 }
Recipe 3-7. Single-Purpose Aggregation Operations(单一整合操作)
准备数据
db.orders.insertMany([
{custID:"10001",amount:500,status:"A"},
{custID:"10001",amount:250,status:"A"},
{custID:"10002",amount:200,status:"A"},
{custID:"10001",amount: 300, status:"D"}]);
1.排除重复custID
db.orders.distinct("custID")
Recipe 3-8. Matching SQL Aggregation to MongoDB Aggregation Operations(SQL与Mongodb的术语匹配)
SQL Term MongoDB Operator
Where $match
groUp BY $group
haVing $match
SeleCt $project
orDer BY $sort
liMit $limit
SUM $sum
CoUnt $sum
Join $lookup
III.Data Modeling and Aggregation(数据模型和整合)相关推荐
- SAP MDG —— 数据建模 Data Modeling
MDG 数据模型 Data Modeling SAP MDG包含以下三大块数据模型: 物料主数据(Material Master) 业务合作伙伴(BP),客户(Customer)和供应商(Suppli ...
- Data Modeling Technologies - ER, IE, Barker, IDEF1X, EXPRES-G, ORM
https://www.cnblogs.com/RicCC/archive/2010/09/13/Data-Modeling-Technologies.html Entity Relationship ...
- 数据建模(Data Modeling)是什么?
声明:译自博客<What is Data Modelling? Conceptual, Logical, & Physical Data Models> 什么是数据建模? 数据建模 ...
- 170. Two Sum III - Data structure design【easy】
170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...
- 合并与拆分数据模型(MSDM:Merging Spliting Data Modeling)
[@more@] MSDM只要针对的是多地区,多维度,跨时区的操作,特别是零售和制造行业,不同地区存在不同的ERP系统和其他同类型不同操作的系统.很多大型的业务系统,有很多的定时进程是在晚上运行,比如 ...
- LeetCode Two Sum III - Data structure design
原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...
- ATH9K Driver Learning Part III: Data packet transmission
在第二篇文章中,我们发现 function ath_tx_start() 会根据 packet 的种类不同而选择不同的发送方式.根据实验结果,我发现只有在 AP-STATION CONNECTION ...
- MySQL Workbench之Data Modeling
建立ER模型 Date Modeling下点击Create New EER Model. 新建Schema,再点击Add Diagram. 3.点击左侧表格图标,在右边空白处点击,出现Table,双击 ...
- mac mysql prefpane_【MySQL数据库开发之一】Mac下配置安装数据库-MySQL
本站文章均为 那么从今天开始陆续会更新数据库和Hibernate框架的博文,也是Himi学习的历程记录,希望大家能共同讨论和研究: OK,本篇简单介绍安装吧,首先到MySQL官方网站: 如上图:点击D ...
- 【MySQL数据库开发之一】Mac下配置安装数据库-MySQL
本站文章均为 李华明Himi 原创,转载务必在明显处注明: 转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/mysql/749.html ☞ 点击订阅 ☜ ...
最新文章
- java重定向设置header_在Java中重定向请求时如何传递HTTP头中的数据
- Transformer 架构逐层功能介绍和详细解释
- Office 365离线安装
- 【攻防世界003】re-for-50-plz-50
- 《敏捷企业》作者访谈录
- [蓝桥杯][2019年第十届真题]修改数组(并查集)
- REVERSE-PRACTICE-BUUCTF-3
- linux hibernate suspend 区别,实现Linux休眠(sleep/hibernate)和挂起(suspend)[转]
- 西瓜书+实战+吴恩达机器学习(二二)概率图模型之马尔可夫随机场
- 基于卷积神经网络与迁移学习的油茶病害图像识别
- vue init失败解决方案-终极版
- 获取位置geolocation 加速度devicemotion	 角度deviceorientation
- 关于Visual Studio 2019安装时VS installer无法下载文件,进度条为0,显示网络有问题的解决办法
- 奥城大学计算机专业,美国研究生双录取的大学及可提供学位详情
- 作用域public,protected,private, 以及不写时的区别
- git提交到主干后,本地将主干代码更新到分支上
- python+大数据-MySQL-day02(黑马)
- daimayuan每日一题#814 排队
- stm32f407小车控制板:电机函数
- linux下git的安装与使用