Mongo汇总将嵌入式文档移到id等于

时间:2020-07-11 00:30:01

标签: javascript angular mongodb flask

美好的一天,尊贵的mongoDB用户。 在一定条件下,在文档级别上移动一个嵌入式文档时出现问题。

此查询:

db.products_oil.aggregate([
{
                '$lookup': {
                    'from': 'brands',
                    'localField': 'brand_id',
                    'foreignField': '_id',
                    'as': 'brand'
                }
            },
            {
                '$lookup': {
                    'from': 'categories',
                    'localField': 'category_id',
                    'foreignField': '_id',
                    'as': 'category'
                }
            },
            {'$unwind': "$brand"},
            {'$unwind': "$category"},
            {
                '$sort': {
                    'position': 1
                }
            }
]).pretty()

给我这样的结构:

{
    "_id" : ObjectId("5f08a990d4a40117fbe1e679"),
    "createdAt" : ISODate("2020-07-10T20:46:56.282Z"),
    "brand_id" : ObjectId("5ef51c165ff59a574439fd0b"),
    "category_id" : ObjectId("5efa20238a909a3d4c52110a"),
    "subCategory_id" : ObjectId("5efa2bc82480962e22ae096d"),
    "position" : 2,
    "active" : 1,
    "productName" : "qwe",
    "productDescription" : "<p>asd</p>",
    "productSpec" : "<p>qwe</p>",
    "productImgPath" : "files/1594403209__me.jpg",
    "productPdf1Path" : "files/pdf/1594403212__20191120-948-cunncj-pdf.pdf",
    "productPdf2Path" : "files/pdf/1594403214__20191120-948-cunncj-pdf.pdf",
    "brand" : {
        "_id" : ObjectId("5ef51c165ff59a574439fd0b"),
        "createdAt" : ISODate("2020-06-26T00:50:14.207Z"),
        "position" : 3,
        "active" : 1,
        "brandImgPath" : "files/1593121813__5.png",
        "brandName" : "xcvxz"
    },
    "category" : {
        "_id" : ObjectId("5efa20238a909a3d4c52110a"),
        "createdAt" : ISODate("2020-06-29T20:08:51.035Z"),
        "position" : 2,
        "active" : 1,
        "categoryType" : 1,
        "categoryName" : "dccccccccc",
        "categoryDescription" : "<p>cccccccccccccccccc</p>",
        "subCategories" : [ 
            {
                "sub_id" : ObjectId("5efa2bc12480962e22ae0969"),
                "createdAt" : ISODate("2020-06-29T20:58:25.894Z"),
                "position" : 3,
                "active" : 1,
                "subCategoryName" : "sscdc",
                "subCategoryDescription" : "<p>csdcsdcsdc</p>"
            }, 
            {
                "sub_id" : ObjectId("5efa2bc82480962e22ae096d"),
                "createdAt" : ISODate("2020-06-29T20:58:32.481Z"),
                "position" : 2,
                "active" : 1,
                "subCategoryName" : "safasdfasdf",
                "subCategoryDescription" : "<p>asfvt3333</p>"
            }, 
            {
                "sub_id" : ObjectId("5efcb3e624f3a0d6351f9b69"),
                "createdAt" : ISODate("2020-07-01T19:03:50.189Z"),
                "position" : 1,
                "active" : 0,
                "subCategoryName" : "asd",
                "subCategoryDescription" : "<p>asd</p>"
            }
        ]
    }
}

我需要将category.subCategories对象作为“子类别”放置到最高级(集合本身的级别),其中 category.subCategories.sub_id === subCategory_id

具有某种:

{
    "_id" : ObjectId("5f08a990d4a40117fbe1e679"),
    "createdAt" : ISODate("2020-07-10T20:46:56.282Z"),
    "brand_id" : ObjectId("5ef51c165ff59a574439fd0b"),
    "category_id" : ObjectId("5efa20238a909a3d4c52110a"),
    "subCategory_id" : ObjectId("5efa2bc82480962e22ae096d"),
    "position" : 2,
    "active" : 1,
    "productName" : "qwe",
    "productDescription" : "<p>asd</p>",
    "productSpec" : "<p>qwe</p>",
    "productImgPath" : "files/1594403209__me.jpg",
    "productPdf1Path" : "files/pdf/1594403212__20191120-948-cunncj-pdf.pdf",
    "productPdf2Path" : "files/pdf/1594403214__20191120-948-cunncj-pdf.pdf",
    "brand" : {
        "_id" : ObjectId("5ef51c165ff59a574439fd0b"),
        "createdAt" : ISODate("2020-06-26T00:50:14.207Z"),
        "position" : 3,
        "active" : 1,
        "brandImgPath" : "files/1593121813__5.png",
        "brandName" : "xcvxz"
    },
    "category" : {
        "_id" : ObjectId("5efa20238a909a3d4c52110a"),
        "createdAt" : ISODate("2020-06-29T20:08:51.035Z"),
        "position" : 2,
        "active" : 1,
        "categoryType" : 1,
        "categoryName" : "dccccccccc",
        "categoryDescription" : "<p>cccccccccccccccccc</p>",
        "subCategories" : [ 
            {
                "sub_id" : ObjectId("5efa2bc12480962e22ae0969"),
                "createdAt" : ISODate("2020-06-29T20:58:25.894Z"),
                "position" : 3,
                "active" : 1,
                "subCategoryName" : "sscdc",
                "subCategoryDescription" : "<p>csdcsdcsdc</p>"
            }, 
            {
                "sub_id" : ObjectId("5efa2bc82480962e22ae096d"),
                "createdAt" : ISODate("2020-06-29T20:58:32.481Z"),
                "position" : 2,
                "active" : 1,
                "subCategoryName" : "safasdfasdf",
                "subCategoryDescription" : "<p>asfvt3333</p>"
            }, 
            {
                "sub_id" : ObjectId("5efcb3e624f3a0d6351f9b69"),
                "createdAt" : ISODate("2020-07-01T19:03:50.189Z"),
                "position" : 1,
                "active" : 0,
                "subCategoryName" : "asd",
                "subCategoryDescription" : "<p>asd</p>"
            }
        ]
    },
    "subcategory" : {
                "sub_id" : ObjectId("5efa2bc82480962e22ae096d"),
                "createdAt" : ISODate("2020-06-29T20:58:32.481Z"),
                "position" : 2,
                "active" : 1,
                "subCategoryName" : "safasdfasdf",
                "subCategoryDescription" : "<p>asfvt3333</p>"
    }, 

}

完全被混乱,小组和项目所困扰:(

2 个答案:

答案 0 :(得分:1)

$filter$addFields应该为您做到这一点:

  {$addFields:{
    subcategory:{
      $arrayElemAt: [
        {$filter:{
           input: "$category.subCategories",
           cond: {$eq:[ "$$this.sub_id", "$subCategory_id" ]}
        }},
        0
      ]
    }
  }}

答案 1 :(得分:1)

您可以使用$reduce

解决此问题
{
  $addFields: {
    subcategory: {
      $reduce: {
        input: "$category.subCategories",
        initialValue: null, // default value in case subcategory is not found
        in: { $cond: [{ $eq: ["$$this.sub_id", "$subCategory_id"] }, "$$this", "$$value"]
        }
      }
    }
  }
}