如何搜索嵌套的字符串数组

时间:2018-04-09 11:47:04

标签: mongodb

我是mongo db的初学者,我在我的数据库中有这个样本

{
    "_id" : ObjectId("5a95cef390bd8fbf1c699d73"),
    "dr_asin" : "0439394422",
    "dr_description" : "Product that encourages families to learn, explore, and create in new and exciting ways.",
    "dr_price" : 12.96,
    "dr_imUrl" : "http://ecx.images-amazon.com/images/I/51Zx2bIwWcL._SX300_.jpg",
    "dr_related" : {
        "also_bought" : [ 
            "B0002667BI", 
            "B00005JKTY", 
            "B0002667B8"
        ],
        "buy_after_viewing" : [ 
            "B00005JKTY", 
            "B00000DGSW", 
            "B0002667BI"
        ]
    },
    "dr_salesRank" : {
        "Video Games" : 36531
    },
    "dr_categories" : [ 
        [ 
            "Video Games", 
            "Mac", 
            "Games"
        ], 
        [ 
            "Video Games", 
            "PC", 
            "Games"
        ]
    ]
}

我想查找Mac类别中的所有产品,我尝试了所有这些

db.P15242570_products.find({dr_categories: {$in: ["Mac"]}})
db.P15242570_products.find( { 'dr_categories.Mac': { $exists: true } } )
db.P15242570_products.find({ dr_categories: "Mac" }
db.P15242570_products.find({ dr_categories : { $all : ['Mac'] }});

但没有任何效果?任何想法?

1 个答案:

答案 0 :(得分:2)

这对我有用

db.15242570_products.find({"dr_categories":{$elemMatch:{$elemMatch:{$in:["PC"]}}});