'UPSERT with Push
I am trying to achieve an upsert with push in Laravel using MongoDB. Basically, I am saving the number of likes from a YouTube post. If the record already exists, I would like to push to an array called 'history'; otherwise, I would like to create the record with all the post details.
Does anyone have any idea how I can achieve this?
I am using this package for the connection with MongoDB. https://github.com/jenssegers/laravel-mongodb/
Thanks.
This is what I want to achieve more precisely: https://user-images.githubusercontent.com/44676430/154502252-0c3dcef4-9bdd-49ae-86fd-3076bc37bbc7.png
Solution 1:[1]
here is an example, the name and author are the key and the quantity is the value.
DB::table('books')->upsert([
[
'name' => 'J.K. Rowling',
'author' => 'Harry Potter',
'quantity' => 15
],
[
'name' => 'Cal Newport',
'author' => 'Deep Work',
'quantity' => 20
]
], ['name', 'author'], ['quantity']);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | andylondon |
