Mongo db - Querying nested array and objects

前端 未结 2 523
醉话见心
醉话见心 2020-12-29 04:29

I have the following document structure:

{
   \"_id\":\"12345\",
   \"value\":{
      \"T\":0,
      \"v\":[
         {
            \"name\":\"JW\",
                 


        
相关标签:
2条回答
  • 2020-12-29 04:54

    It's not clear exactly what you tried, but this should work to find the above doc by name:

    db.collection.find({ "value.v.name": "JW" })
    

    Reference

    0 讨论(0)
  • 2020-12-29 05:00

    You should use $elemMatch operator:

    db.collection.find({
        'value.v': { 
            $elemMatch: {
                name: 'JW', // "name == 'JW'"
                cost : 100 //if you need "&& cost == 100"
            }
        }
    });
    

    Mongo docs

    0 讨论(0)
提交回复
热议问题