Search code examples
databaserethinkdbrethinkdb-javascript

RethinkDB Update the element in the nested array


{
   id: "a",
   deck_list: [{
      name: 'Deck1',
      job: 'mage',
      cards: []
   }],
   match: []
 }

Hi I am trying to make a DB for card game Decks. In 'deck_list', there are list of decks that created by users. Whenever user adds a new deck, then it would be inserted into deck_list.

However, when the name of the deck is already there, then the deck should be updated, rather than inserted.

Ex. If some deck named 'Deck2' is inserted, then it should be added to form

{
   id: "a",
   deck_list: [{
      name: 'Deck1',
      job: 'mage',
      cards: []
   },
   {
      name: 'Deck2',
      job: 'mage',
      cards: []
   }],
   match: []
 }

But when 'Deck1' is added, then old 'Deck1' should be replaced with newer 'Deck1'.


Solution

  • You probably want to make deck_list an object rather than an array, and map from the name of the deck to its job/cards. Then you can use update normally, and it will create the deck if it doesn't exist or update it if it does.