【问题标题】:Mongo aggregate move embedded document to collection where id equalsMongo 聚合将嵌入文档移动到 id 等于的集合
【发布时间】:2020-07-11 00:30:01
【问题描述】:

您好,尊贵的 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>"
    }, 

}

完全搞乱了放松、小组和项目:(

【问题讨论】:

    标签: javascript angular mongodb flask


    【解决方案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"]
            }
          }
        }
      }
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 这是一个干净整洁的解决方案正在工作!感谢您的宝贵时间,先生!
      猜你喜欢
      • 2011-07-19
      • 2012-12-06
      • 1970-01-01
      • 2021-03-28
      • 1970-01-01
      • 2013-09-18
      • 2019-05-20
      • 1970-01-01
      • 2018-03-28
      相关资源
      最近更新 更多