$addFields 没有在文档中添加值

发布于 2025-01-13 03:31:11 字数 2863 浏览 0 评论 0原文

查询如下,结果如下:
我想要的是添加名为 name 的字段,其中我想要categoryObj[0].categoryName 但它是空的。
尝试categoryObj.$.categoryName但出现错误。
一旦获得我想要的名称,我将使用项目操作符排除categoryObj。
提前感谢帮助

let itemsByCategory = await VendorItem.aggregate([            
            {$match: {vendor: vendorId}},
            {$lookup: {
                from: "vendorcategories",
                localField: "category",
                foreignField: "_id",
                as: 'categoryDetails'
            }},
            {$group:{
                "_id":"$category",
                "count":{"$sum":1},
                "items":{"$push":"$$ROOT"},
                "categoryObj":{"$addToSet":"$categoryDetails"}
            }},
            {$project: {"items.categoryDetails":0}},
            {$addFields: {"categoryName" : "$categoryObj.categoryName"}},
            //{$project: {"categoryObj":0}},
        ]);

,结果如下

{
    "itemsByCategory": [
        {
            "_id": "62296d612a1462a7d5e4b86b",
            "count": 1,
            "menuItems": [
                {
                    "_id": "622971fa4fda7b4c792a7812",
                    "category": "62296d612a1462a7d5e4b86b",
                    "vendor": "62296c6f2a1462a7d5e4b863",
                    "item": "Dahi Chaat",
                    "price": 30,
                    "inStock": true,
                    "variants": [
                        {
                            "variantName": "With Sev",
                            "variantPrice": 40,
                            "_id": "622975b9f7bdf6c2a3b7703c"
                        }
                    ],
                    "toppings": [
                        {
                            "name": "cheese",
                            "price": 10,
                            "inStock": true,
                            "_id": "62297766ff9f01d236c60736"
                        }
                    ],
                    "categoryDetails": [
                        {
                            "_id": "62296d612a1462a7d5e4b86b",
                            "categoryName": "Snacks",
                            "categoryDescription": "Desciption changed!",
                            "vendor": "621c6c944d6d79e83219e59a",
                            "__v": 0
                        }
                    ]
                }
            ],
            "categoryObj": [
                [
                    {
                        "_id": "62296d612a1462a7d5e4b86b",
                        "categoryName": "Snacks",
                        "categoryDescription": "Desciption changed!",
                        "vendor": "621c6c944d6d79e83219e59a",
                        "__v": 0
                    }
                ]
            ],
            "name": []
        }
    ]
}

Query is as follows and result is given below:

What I want is I am adding field called name, in which I want categoryObj[0].categoryName but it is empty.

Tried categoryObj.$.categoryName but giving error.

Once name is obtained as I want i will exclude categoryObj with project opertator.

Thanks for help in advance

let itemsByCategory = await VendorItem.aggregate([            
            {$match: {vendor: vendorId}},
            {$lookup: {
                from: "vendorcategories",
                localField: "category",
                foreignField: "_id",
                as: 'categoryDetails'
            }},
            {$group:{
                "_id":"$category",
                "count":{"$sum":1},
                "items":{"$push":"$ROOT"},
                "categoryObj":{"$addToSet":"$categoryDetails"}
            }},
            {$project: {"items.categoryDetails":0}},
            {$addFields: {"categoryName" : "$categoryObj.categoryName"}},
            //{$project: {"categoryObj":0}},
        ]);

and the result is as follows

{
    "itemsByCategory": [
        {
            "_id": "62296d612a1462a7d5e4b86b",
            "count": 1,
            "menuItems": [
                {
                    "_id": "622971fa4fda7b4c792a7812",
                    "category": "62296d612a1462a7d5e4b86b",
                    "vendor": "62296c6f2a1462a7d5e4b863",
                    "item": "Dahi Chaat",
                    "price": 30,
                    "inStock": true,
                    "variants": [
                        {
                            "variantName": "With Sev",
                            "variantPrice": 40,
                            "_id": "622975b9f7bdf6c2a3b7703c"
                        }
                    ],
                    "toppings": [
                        {
                            "name": "cheese",
                            "price": 10,
                            "inStock": true,
                            "_id": "62297766ff9f01d236c60736"
                        }
                    ],
                    "categoryDetails": [
                        {
                            "_id": "62296d612a1462a7d5e4b86b",
                            "categoryName": "Snacks",
                            "categoryDescription": "Desciption changed!",
                            "vendor": "621c6c944d6d79e83219e59a",
                            "__v": 0
                        }
                    ]
                }
            ],
            "categoryObj": [
                [
                    {
                        "_id": "62296d612a1462a7d5e4b86b",
                        "categoryName": "Snacks",
                        "categoryDescription": "Desciption changed!",
                        "vendor": "621c6c944d6d79e83219e59a",
                        "__v": 0
                    }
                ]
            ],
            "name": []
        }
    ]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

土豪我们做朋友吧 2025-01-20 03:31:11

您可以添加 $unwind 阶段以便“循环”“categoryObj”内的所有对象,但之后您需要将其分组:

{"$addFields": {orig_id: "$_id"}},
{"$unwind": "$categoryObj"},
{"$addFields": {"name": {"$arrayElemAt": ["$categoryObj", 0]}}},
{"$group": {_id: "$orig_id",  name: {$push: "$name.categoryName"},
  menuItems: {$first: "$menuItems"}, count: {$first: "count"},
}

}

请参阅此处的游乐场:
https://mongoplayground.net/p/wsH2Y0UZ_FH

You can add an $unwind phase in order to "loop" all objects inside "categoryObj", but you will need to group it back afterwards:

{"$addFields": {orig_id: "$_id"}},
{"$unwind": "$categoryObj"},
{"$addFields": {"name": {"$arrayElemAt": ["$categoryObj", 0]}}},
{"$group": {_id: "$orig_id",  name: {$push: "$name.categoryName"},
  menuItems: {$first: "$menuItems"}, count: {$first: "count"},
}

}

See playground here:
https://mongoplayground.net/p/wsH2Y0UZ_FH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文