0

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

  • Can you explain in more detail? – Wahyu Kristianto Feb 14 '22 at 20:41
  • https://user-images.githubusercontent.com/44676430/154502252-0c3dcef4-9bdd-49ae-86fd-3076bc37bbc7.png – Constantine Lamar Feb 17 '22 at 14:30
  • Stack Overflow is not a code writing service. We are always glad to help and support new coders but you need to help yourself first. You are expected to try to write the code yourself. Please take the [tour](https://stackoverflow.com/tour), read [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). – Wahyu Kristianto Feb 17 '22 at 15:21

1 Answers1

-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']);
andylondon
  • 176
  • 6