MongoDB remove an item from an array inside an array of objects

后端 未结 1 1020
悲&欢浪女
悲&欢浪女 2021-01-12 01:01

I have a document that looks like this:

{
  \"_id\" : ObjectId(\"56fea43a571332cc97e06d9c\"),
  \"sections\" : [
    {
      \"_id\" : ObjectId(\"56fea43a571         


        
1条回答
  •  鱼传尺愫
    2021-01-12 01:13

    You need to use the positional $ update operator to remove the element from your array. You need this is because "sections" is an array of sub-documents.

    db.test.findOneAndUpdate(
        { "sections._id" : ObjectId("56fea43a571332cc97e06d9e") }, 
        { "$pull": { "sections.$.registered": "e3d65a4e-2552-4995-ac5a-3c5180258d87" } } 
    )
    

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