pull operator

161 views
Skip to first unread message

Yoni Obadia

unread,
Nov 19, 2020, 4:04:19 AM11/19/20
to kmongo
Hello, I'm trying to update the content of my array, I've been able to do it using mongo CLI with the following command:

db.myCol.update(
{ _id : ObjectId("123456788")}, 
{ $pull: { 
"alert_list": { 
"author_id": "12345678"
                } 
} }
)

However when I try to convert it in KMongo it doesn't work

(here myColId and authorId are variable from my function)
myCollection.updateOne(
    MyCol::id eq myColId
    pull(MyCol::alertList / MyCol.Alert::authorId, authorId)
)

Did I miss something?

Yoni Obadia

unread,
Nov 19, 2020, 1:33:13 PM11/19/20
to kmongo
To add more details, the document I'm trying to modify look as follow:

{
_id" : ObjectId("5fb6287e4dca474385ca093b"),
"debug" : true,
"authorized_group" : NumberLong("876598712309302053"),
"watch_list" : [
...
],
"alert_list" : [
{
"author_id" : "123456789",
"alert_date" : 1592591543,
"is_premium" : false
},
...
]
}

zigzago

unread,
Nov 19, 2020, 10:29:39 PM11/19/20
to kmongo
Hello,

pull is for simple equality (if you had . "alert_list": { "author_id": ["123456789"] } )

Here you have to use pullByFilter:   pullByFilter(MyCol::alertList / MyCol.Alert::authorId eq authorId)

HTH
Message has been deleted

Yoni Obadia

unread,
Nov 20, 2020, 2:43:21 AM11/20/20
to kmongo
Hello ! 

Thanks for your answer, I've tried that but I got the following error message: Cannot use the part (author_id) of (alert_list.author_id) to traverse the element ({alert_list:....)
So I tried to add posOp and allPosOp operator to fix this but it didn't work

return myCol.updateOne(MyCol::id eq id, pullByFilter(MyCol::alertList.posOp / MyCol.Alert::authorId eq authorId))
> The positional operator did not find the match needed from the query.

return myCol.updateOne(MyCol::id eq id, pullByFilter(MyCol::alertList.allPosOp / MyCol.Alert::authorId eq authorId))
> Cannot apply $pull to a non-array value

zigzago

unread,
Nov 21, 2020, 3:42:23 PM11/21/20
to kmongo
Hi,

The solution is:

pullByFilter(MyCol::alertList, Alert::authorId eq "1") // translated as {"$pull": {"alert_list": {"author_id": "1"}}}

Because pullByFilter(MyCol::alertList / Alert::authorId eq "1") is translated as {"$pull": {"alert_list.author_id": "1"}}


HTH

Yoni Obadia

unread,
Nov 22, 2020, 5:20:11 PM11/22/20
to kmongo
oh thank you, I didn't know that I could use a comma.
Reply all
Reply to author
Forward
0 new messages