This is the document I currently have:
{
"_id" : ObjectId("5adef141502f49a6a2812016"),
"productId" : "123456",
"users" : [
{
"userId" : "1111111",
"internalUserId" : "azxcvdd"
},
{
"userId" : "2222222",
"internalUserId" : "dsdasd"
}
]
}
I would like the push new user to the array if is not exists, And if the userId exists and the internalUserId different I want to update it
So if i got userId:"1111111" and internalUserId:"bbbb" The doc will be:
{
"_id" : ObjectId("5adef141502f49a6a2812016"),
"productId" : "123456",
"users" : [
{
"userId" : "1111111",
"internalUserId" : "bbbb"
},
{
"userId" : "2222222",
"internalUserId" : "dsdasd"
}
]
}
and if i got userId:"44444" and internalUserId:"bbbb" The doc will be
{
"_id" : ObjectId("5adef141502f49a6a2812016"),
"productId" : "123456",
"users" : [
{
"userId" : "1111111",
"internalUserId" : "bbbb"
},
{
"userId" : "2222222",
"internalUserId" : "dsdasd"
},
{
"userId" : "44444",
"internalUserId" : "bbbb"
}
]
}
I tryid to do something like that:
val selector = Json.obj("productId" -> productId)
val modifier = Json.obj("$addToSet" -> Json.obj("users" -> Json.obj("userId" -> user.userId, "internalUserId" -> user.internalUserId)))
for {
collection <- collectionFut
writeResult <- collection.findAndUpdate(selector, modifier, upsert = true)
But In a situation that is the internalUserId different it adds new item not update it
Thanks!