Reputation: 4810
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?
Upvotes: 1
Views: 95
Reputation: 521
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
}
Upvotes: 1
Reputation: 4810
Update monggoes 5.13 and do belove code.
"channelDetail.$[].thingchannels": { $in: ["x","y"]},
Upvotes: 0