~~~
//users
{
name:"chengchao",name_id:1,
name:"jiangwei",name_id:2
}
~~~
~~~
//details
{school:"武大",detail_id:1},
{school:"华科",detail_id:2}
~~~
对details遍历,当detail_id等于users的name_id时,让detail对应id的document作为users对应id的属性
~~~
const MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/';
MongoClient.connect(url, {
useNewUrlParser: true
}, (err, client) => {
if (err) throw err;
var test = client.db('test');
test.collection('user').aggregate([
{ $lookup:
{
from: 'detail',
localField: 'name_id',
foreignField: 'detail_id',
as: 'details'
}
}
]).toArray((err,result)=>{
if(err) throw err;
console.log(JSON.stringify(result));
client.close()
})
})
~~~
- MongoDB
- 第一章开发环境配置
- 第二章 基础操作
- 2-1 create-collection
- 2-2 collection-insert
- 2-3 find
- 2-4 query
- 2-5 sort排序
- 2-6 delete
- 2-7 drop-collection
- 2-8 update
- 2-9 limit
- 2-10 join
- 2-10-1 返回json给前台
- 2-11 ObjectId
- 第三章 数据库封装
- 3-0 数据库封装思路
- 3-1 单例
- 3-2 增加数据的执行时间
- 3-1-1 查询耗时
- 3-1-2 数据连接示例
- 3-3 简单封装
- 3-4 二次封装
- 3-5 结合art-template使用
- 3-6 数据库封装终极
- Redis
- 第一章 开发环境配置