如何使用猫鼬只返回第三级的子文档?

时间:2019-05-04 23:32:18

标签: mongodb

我具有以下文档结构:

{ 
    "_id" : ObjectId("5ccdccc61b704939740f6ad7"), 
    "Keywords" : [
        "Dine Out", 
        "Dine Out"
    ], 
    "NameAR" : "Dine Out", 
    "NameEN" : "Dine Out", 
    "DescriptionAR" : "Dine Out", 
    "DescriptionEN" : "Dine Out", 
    "Type" : "main", 
    "SubCategories" : [
        {
            "Keywords" : [
                "Fast food", 
                "Fast food"
            ], 
            "_id" : ObjectId("5ccdccc61b704939740f6adb"), 
            "NameAR" : "Fast food", 
            "NameEN" : "Fast food", 
            "DescriptionAR" : "Fast food", 
            "DescriptionEN" : "Fast food", 
            "Type" : "Outlet", 
            "SubCategories" : [
                {
                    "Keywords" : [

                    ], 
                    "_id" : ObjectId("5ccdccc61b704939740f6add"), 
                    "NameAR" : "Burger", 
                    "NameEN" : "Burget", 
                    "DescriptionAR" : "Burget", 
                    "DescriptionEN" : "Burget", 
                    "Type" : "Item", 
                    "SubCategories" : [

                    ]
                }, 
                {
                    "Keywords" : [

                    ], 
                    "_id" : ObjectId("5ccdccc61b704939740f6adc"), 
                    "NameAR" : "Fries", 
                    "NameEN" : "Fries", 
                    "DescriptionAR" : "Fries", 
                    "DescriptionEN" : "Fries", 
                    "Type" : "Item", 
                    "SubCategories" : [

                    ]
                }
            ]
        }, 
        {
            "Keywords" : [
                "Coffee Shops", 
                "Coffee Shops"
            ], 
            "_id" : ObjectId("5ccdccc61b704939740f6ad8"), 
            "NameAR" : "Coffee Shops", 
            "NameEN" : "Coffee Shops", 
            "DescriptionAR" : "Coffee Shops", 
            "DescriptionEN" : "Coffee Shops", 
            "Type" : "Outlet", 
            "SubCategories" : [
                {
                    "Keywords" : [
                        "black coffee", 
                        "green tea"
                    ], 
                    "_id" : ObjectId("5ccdccc61b704939740f6ada"), 
                    "NameAR" : "Coffee", 
                    "NameEN" : "Coffee", 
                    "DescriptionAR" : "Coffee", 
                    "DescriptionEN" : "Coffee", 
                    "Type" : "Item", 
                    "SubCategories" : [

                    ]
                }, 
                {
                    "Keywords" : [

                    ], 
                    "_id" : ObjectId("5ccdccc61b704939740f6ad9"), 
                    "NameAR" : "cheese cake", 
                    "NameEN" : "cheese cake", 
                    "DescriptionAR" : "cheese cake", 
                    "DescriptionEN" : "cheese cake", 
                    "Type" : "Item", 
                    "SubCategories" : [

                    ]
                }
            ]
        }
    ], 
    "__v" : NumberInt(0)
}

我已完成以下查询:

Category.findOne({ "SubCategories.SubCategories.Keywords": "black coffee" });

该查询工作正常,但返回了整个文档,我只想返回第3级的以下对象。

{
                    "Keywords" : [
                        "black coffee", 
                        "green tea"
                    ], 
                    "_id" : ObjectId("5ccdccc61b704939740f6ada"), 
                    "NameAR" : "Coffee", 
                    "NameEN" : "Coffee", 
                    "DescriptionAR" : "Coffee", 
                    "DescriptionEN" : "Coffee", 
                    "Type" : "Item", 
                    "SubCategories" : [

                    ]
                }

我尝试过

Category.findOne({{“ SubCategories.SubCategories.Keywords”:“黑咖啡”},{'SubCategories.SubCategories。$ ._ id':1});

,但它仅返回第二级,而不是第三级。有什么线索吗?

0 个答案:

没有答案