mongodb - MongoDb 文档与内部对象连接
问题描述
我有 2 个名为 call 和客户的文档,我想将 locationId 加入到customers.location._id 的调用中
来电
"_id": "5d456560221b9147e4af4b97",
"customerId": "5d0a3bbfac9470aea56ad619",
"locationId": "5d11d2c4cba8151f67f735b1",
"startDateTime": "2019-06-21T05:30:00.000Z",
"endDateTime": "2019-06-21T06:00:00.000Z"
}
顾客
"_id": "5d0a3bbfac9470aea56ad619",
"locations": [
{
"_id": "5d11d2c4cba8151f67f735b1",
"phone": "9889599999",
"ext": "91",
"address": "sad",
"neighborhood": "sda",
"city": "punetest",
"state": "MH",
"zip": "589365",
"country": "india",
"isBillingAddress": false,
"taxPercentage": 0,
"type": "Residential",
"status": "Active",
"firstName": "A",
"lastName": "B"
},
{
"_id": "5d11e457cba8151f67f735b3",
"phone": "969696999",
"ext": "91",
"address": "",
"neighborhood": "",
"city": "udaipur",
"state": "raj",
"zip": "312563",
"country": "india",
"isBillingAddress": false,
"taxPercentage": 0,
"type": "Residential",
"status": "Deleted",
"firstName": "AB",
"lastName": "CD"
}
],
"createdAt": "2019-06-19T13:42:23.479Z",
"updatedAt": "2019-06-25T13:39:07.597Z"
}
[
{
$lookup: {
from: 'customers.locations',
localField: 'locationId',
foreignField: '_id',
as: 'customers.locations',
},
}
]);
它不起作用我有 2 个名为 call 和客户的文档,我想将 locationId 的呼叫加入到 customers.location._id
我想要输出``` { "_id": "5d456560221b9147e4af4b97", "customerId": "5d0a3bbfac9470aea56ad619", "locationId": "5d11d2c4cba8151f67f735b1", "location":{"_id": "5d11d2c4cba8151f69788""phone" ", "ext": "91", "address": "sad", "neighborhood": "sda", "city": "punetest", "state": "MH", "zip": "589365", “国家”:“印度”,“isBillingAddress”:假,“taxPercentage”:0,“类型”:“住宅”,“状态”:“活动”,“名字”:“A”,“姓氏”:“B”}}
解决方案
好的,这就是您正在查看的内容:
如果您locationId
的来电中只有一个匹配customer.locations._id
的客户:
db.getCollection('calls').aggregate([
{
$lookup: {
from: 'customers',
localField: 'locationId',
foreignField: 'locations._id',
as: 'customersCalls',
}
}, { $unwind: '$customersCalls' },
{
$project: {
customerId: 1, locationId: 1, locations: {
$filter: {
input: "$customersCalls.locations",
as: "item",
cond: { $eq: ["$$item._id", '$locationId'] }
}
}
}
},
{ $group: { _id: '$_id', result: { $first: '$$ROOT' } } },
{ $replaceRoot: { newRoot: "$result" } }
])
否则,如果您的locationId
in呼叫customer.locations._id
在customer中有多个匹配项:
db.getCollection('calls').aggregate([
{
$lookup: {
from: 'customers',
localField: 'locationId',
foreignField: 'locations._id',
as: 'customersCalls',
}
}, { $unwind: '$customersCalls' },
{
$project: {
customerId: 1, locationId: 1, locations: {
$filter: {
input: "$customersCalls.locations",
as: "item",
cond: { $eq: ["$$item._id", '$locationId'] }
}
}
}
},
{ $group: { _id: '$_id', locations: { $push: { $arrayElemAt: ["$locations", 0] } }, result: { $first: '$$ROOT' } } },
{ $addFields: { 'result.locations': '$locations' } },
{ $replaceRoot: { newRoot: "$result" } }
])
推荐阅读
- javascript - 从两个字段计算值并显示在 JavaScript 的第三个字段中
- machine-learning - 我将如何计算非线性(rbf 内核)svm 中距超平面/决策边界的边距距离?
- flutter - 如何在没有 Cupertino 底栏的情况下路由到新页面
- c# - 带有 TextWrap 的单元格行不会使用 EPPlus 自动调整大小
- architecture - 构建身份验证服务所需的架构
- tensorflow - 名称 tf.Session 已弃用。请改用 tf.compat.v1.Session
- javascript - 仅在数字相等时才发现如何修复 HCF
- scala - 在控制器的方法中不使用列表中的头部
- bootstrap-4 - 我有一个关于将元素向右推的问题
- docker - `docker-compose run client yarn lint`与`cd client && yarn lint`有什么区别