1

I have the below document in mongoose. And I want to pull only strings instead of delete objects.

[{
  "_id": {
    "$oid": "6051e87aa8698c13fb4951fa"
  },
  "user": {
    "$oid": "6051e87aa8698c13fb4951f7"
  },
  "channelDetail": [
    {
      "hiddenchannels": [],
      "thingchannels": [],
      "_id": {
        "$oid": "6051e87aa8698c13fb4951fb"
      },
      "room": {
        "$oid": "6051e87aa8698c13fb4951f9"
      }
    },
    {
      "room": {
        "$oid": "60a8e3f685b8741eaef56e25"
      },
      "_id": {
        "$oid": "60e03c7076b5d3915dad1c3b"
      },
      "thingchannels": [
        {
          "$oid": "60e0158af5f1cf131bbb7be6"
        },
        {
          "$oid": "60e0158af5f1cf131bbb7be7"
        },
        {
          "$oid": "60e0158af5f1cf131bbb7be8"
        },
        {
          "$oid": "60e0158af5f1cf131bbb7be9"
        }
      ],
      "hiddenchannels": []
    },
    {
      "hiddenchannels": [],
      "thingchannels": [],
      "_id": {
        "$oid": "60e02c651b08eb18ac565770"
      },
      "room": {
        "$oid": "60e02c5c1b08eb18ac56576f"
      }
    }
  ],
  "__v": 13,
  "localUpdateAt": 1625306846846
}]

Now I want to delete a single string in thingchannels array, but the query returns the full object instead of delete string.

await this.roomUserDetail.updateMany(
        { "channelDetail.thingchannels": { $in: ["60e0158af5f1cf131bbb7be6"] } },
        {
          $pull: {
            channelDetail: {
              thingchannels: { $in: ["60e0158af5f1cf131bbb7be6"] },
            },
          },
        }
      );

Is there any way to delete only a string inside the string array?

8
  • This may help: stackoverflow.com/questions/5228210/… Commented Jul 3, 2021 at 11:31
  • @CuongLeNgoc does not work because I have * thingchannels* is object Array. so $pull not accept. Please see error ibb.co/fGVYxXT Commented Jul 3, 2021 at 11:35
  • Which mongoose version do you use? Can you update it to lastest version? Commented Jul 3, 2021 at 11:41
  • @CuongLeNgoc "version": "4.4.1" Commented Jul 3, 2021 at 11:54
  • If so it's not compatible with that solution. Cam you update mongoose and mongodb? Commented Jul 3, 2021 at 12:02

2 Answers 2

1

Try following, I also tried it, working fine.

await this.roomUserDetail.updateMany({ 
  "channelDetail.thingchannels": { 
    $in: [mongoose.Types.ObjectId("60e0158af5f1cf131bbb7be6")] 
  } 
},{
  $pull:{
    "channelDetail.$[].thingchannels":{
      $in:[mongoose.Types.ObjectId("60e0158af5f1cf131bbb7be6")]
    }
  }
})

OUTPUT after query run:

{
    "user": {
        "$oid": "6051e87aa8698c13fb4951f7"
    },
    "channelDetail": [{
        "hiddenchannels": [],
        "thingchannels": [],
        "_id": {
            "$oid": "6051e87aa8698c13fb4951fb"
        },
        "room": {
            "$oid": "6051e87aa8698c13fb4951f9"
        }
    }, {
        "room": {
            "$oid": "60a8e3f685b8741eaef56e25"
        },
        "_id": {
            "$oid": "60e03c7076b5d3915dad1c3b"
        },
        "thingchannels": [{
            "$oid": "60e0158af5f1cf131bbb7be7"
        }, {
            "$oid": "60e0158af5f1cf131bbb7be8"
        }, {
            "$oid": "60e0158af5f1cf131bbb7be9"
        }],
        "hiddenchannels": []
    }, {
        "hiddenchannels": [],
        "thingchannels": [],
        "_id": {
            "$oid": "60e02c651b08eb18ac565770"
        },
        "room": {
            "$oid": "60e02c5c1b08eb18ac56576f"
        }
    }],
    "__v": 13,
    "localUpdateAt": 1625306846846
}
Sign up to request clarification or add additional context in comments.

Comments

0

Update monggoes 5.13 and do belove code.

"channelDetail.$[].thingchannels": { $in: ["x","y"]},

Comments

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.