2

我创建了一个聚合管道,其执行以下操作:把整个原始文件在一个新的属性名称

  1. 转到自定义收集和_id
  2. 去找到默认的收集,并通过ID查找,将其附加由ID为默认
  3. 转到风格的收集和发现,重视它作为风格

代码:

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 /猫鼬。

回答

2

个人而言,我只想$project$$ROOT为“在一开始就”管道的$match后的新名称:

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进行了介绍。

+1

这种方法比我建议的更好,更简单。 –

+0

这个伎俩! – Moshe

1

有可能是一个更好的办法,但在管道末端使用$replaceRoot应该得到你所需要的结构(未经测试):

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' 
    }, 

    { 
     $replaceRoot: { 
      newRoot: { 
       "custom": { 
        "_id": "$_id", 
        "updatedAt": "$updatedAt", 
        "createdAt": "$createdAt", 
        "styleId": "$styleId", 
        "colorId": "$colorId", 
        "selectedOption": "$selectedOption", 
        "deselectedOption": "$deselectedOption", 
        "currentOptions": "$currentOptions", 
        "requiredItems": "$requiredItems", 
        "includedItems": "$includedItems", 
        "furtherRemovals": "$furtherRemovals", 
        "furtherAdditions": "$furtherAdditions", 
        "excludedItems": "$excludedItems", 
        "name": "$name", 
        "__v": "$__v", 
       }, 
       "style": "$style", 
       "default": "$default" 

      } 
     } 
    } 

]).then(docs => res.json(docs)) 
} 

要使用此功能,您蒙戈版本应为3.4或更高版本。

+1

为什么要使用一个限制于MongoDB 3.4 **的操作符,并且使用一种非常简洁的语法来实际不需要?你这样做的方式实际上可以用一个标准的'$ project'并且不需要'$ replaceRoot',但是也有一个**更简单的**方法。 –

+1

谢谢@NeilLunn –

+0

@TalhaAwan感谢您的回复。这使Neil能够提供我非常需要的答复。感谢您的推动! – Moshe

相关问题