gpt4 book ai didi

node.js - 将整个原始文档置于新的属性名称下

转载 作者:可可西里 更新时间:2023-11-01 10:02:29 24 4
gpt4 key购买 nike

我创建了一个聚合管道,它执行以下操作:

  1. 转到自定义集合并按 _id 查找
  2. 转到默认集合并按 id 查找,将其附加为默认值
  3. 转到 Style collection 并通过 id 查找,将其附加为 Style

代码:

Custom.aggregate([{
$match: {
'_id': mongoose.Types.ObjectId(req.params.id)
}
},
{
$lookup: {
from: 'styles',
localField: 'styleId',
foreignField: 'id',
as: 'style'
}
},
{
$unwind: '$style'
},
{
$lookup: {
from: 'default',
localField: 'styleId',
foreignField: 'id',
as: 'default'
}
},
{
$unwind: '$default'
},
]).then(docs => res.json(docs))
}

这是当前输出:

{
"_id": "597774cc09064e2e4e9fedb7",
"updatedAt": "2017-07-25T16:41:48.823Z",
"createdAt": "2017-07-25T16:41:48.823Z",
"styleId": "401648805",
"colorId": null,
"selectedOption": null,
"deselectedOption": null,
"currentOptions": [],
"requiredItems": [],
"includedItems": [],
"furtherRemovals": [],
"furtherAdditions": [],
"excludedItems": [],
"name": [],
"__v": 0,
"style": {
"_id": "5902276effabb8bd2926be71",
"id": "401648805",
"name": "Luxury 4dr Sedan (2.0L 4cyl Turbo 8A)",
"createdAt": "2017-07-26T15:28:36.778Z",
"updatedAt": "2017-07-26T15:49:14.366Z"
},
"default": {
"_id": "5978bfef32d246877e9cb363",
"updatedAt": "2017-07-26T16:14:39.779Z",
"createdAt": "2017-07-26T16:14:39.779Z",
"id": "401648805",
"requiredItems": [],
"includedItems": [],
"furtherRemovals": [],
"furtherAdditions": [],
"excludedItems": [],
"__v": 0
}
}

我试图将我的自定义对象包装在一个“自定义”命名对象中,它更适合我们的数据结构:

{
"custom": {
"_id": "597774cc09064e2e4e9fedb7",
"updatedAt": "2017-07-25T16:41:48.823Z",
"createdAt": "2017-07-25T16:41:48.823Z",
"styleId": "401648805",
"colorId": null,
"selectedOption": null,
"deselectedOption": null,
"currentOptions": [],
"requiredItems": [],
"includedItems": [],
"furtherRemovals": [],
"furtherAdditions": [],
"excludedItems": [],
"name": [],
"__v": 0,
},
"style": {
"_id": "5902276effabb8bd2926be71",
"id": "401648805",
"name": "Luxury 4dr Sedan (2.0L 4cyl Turbo 8A)",
"createdAt": "2017-07-26T15:28:36.778Z",
"updatedAt": "2017-07-26T15:49:14.366Z"
},
"default": {
"_id": "5978bfef32d246877e9cb363",
"updatedAt": "2017-07-26T16:14:39.779Z",
"createdAt": "2017-07-26T16:14:39.779Z",
"id": "401648805",
"requiredItems": [],
"includedItems": [],
"furtherRemovals": [],
"furtherAdditions": [],
"excludedItems": [],
"__v": 0
}
}

我认为它与聚合管道中的 $group 有关,但我不确定如何构建它。任何帮助将不胜感激 - MonogoDB/Mongoose 相对较新。

最佳答案

就我个人而言,我会 $project$match 之后将 $$ROOT 更改为管道的新名称“就在开头” :

Custom.aggregate([
{ $match: {
'_id': mongoose.Types.ObjectId(req.params.id)
}},
{ $project: { _id: 0, custom: '$$ROOT' } },
{ $lookup: {
from: 'styles',
localField: 'custom.styleId',
foreignField: 'id',
as: 'style'
}},
{ $unwind: '$style' },
{ $lookup: {
from: 'default',
localField: 'custom.styleId',
foreignField: 'id',
as: 'default'
}},
{ $unwind: '$default' }
]).then(docs => res.json(docs))

它是最少的代码并且兼容自$lookup以来的所有版本被介绍。

关于node.js - 将整个原始文档置于新的属性名称下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45333504/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com