0

I am a beginner in mongo db, I have this sample in my database

{
    "_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"
        ]
    ]
}

I want to find all products that are in the Mac category, I tried all these

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'] }});

but nothing is working ? any ideas ?

2
  • This is a duplicate of stackoverflow.com/questions/29071748/… Commented Apr 9, 2018 at 11:49
  • @MohitMutha I don't think so, I tried that solution and it didn't work, my problem is that the arrays have no names Commented Apr 9, 2018 at 11:51

1 Answer 1

2

This worked for me

db.15242570_products.find({"dr_categories":{$elemMatch:{$elemMatch:{$in:["PC"]}}});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! it worked! just had to add } at the end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.