简体   繁体   中英

How to pull data from deeply nested array in mongodb?

Hi I have deeply nested array in my mongoose schema. I want to pull record from that deeply nested array. Can anyone please help in writing the moongoose query. I tried this

var condition = {
                    _id: user,
                    $and: [
                        {
                            'cardInfo.cards': {
                                $elemMatch: {
                                    _id: cardId,
                                    isActive: '0'
                                }
                            }
                        },
                        {
                            'cardInfo.cards': {
                                $elemMatch: {
                                    _id: { $exists: true }
                                }
                            }
                        }
                    ]

                };
 var dataToUpdate = {
      $pull: {'cardInfo.cards': { _id: cardId }}
 };
 var options = {
      lean: true
 }

for schema please look at MyAnotherQuestion and please try to answer that question as well. Above query is not working but in mongodb it is working fine if I use ObjectId for cardId

Ok I have been able to resolve the issue. What I did is just added an another parameter in options variable like:

var options = { strict: false, lean: true}

strict: false is the parameter which made my query to work and my mongoose query is same ie

Customer.update(condition, dataToUpdate , options, anycallback)

and yes it is working for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM