0

I want to query the specific object by the property "id" inside the array and remove/delete the whole object. Is this possible through firestore?

My firestore structure looks like this:

enter image description here

For now I have function called removeCard() that takes in the docId which is the document id in firestore, and the cardId which is property "id" value inside the object.

removeCard() function:

export const removeCard = (retroId, cardId) => {
  return (dispatch, getState, { getFirestore }) => {
    // make async call to database
    const firestore = getFirestore();

    firestore.collection('retros').doc(retroId).update({
      // firestore query to remove/delete this card.
    }).then(() => {
      dispatch({ type: 'DELETE_CARD_SUCCESS' });
    }).catch(err => {
      dispatch({ type: 'DELETE_CARD_ERROR' }, err);
    });
  }
};

But I cant figure out how to do this query or if it's even possible to do with Firestore. Any help is appreciated. Just tell me if i should provide with any more information to make my question more clear.

1 Answer 1

1

Actually, at the time of writing, it is not possible, with array-contains, to query for a specific property of an object stored in an array.

A possible workaround exists, which is to query for the entire object.

In your case it would be like

db.collection('retros').where("cardsAction", "array-contains", {id: "65....", content: "this is...", time: ....})

but obviously this workaround will not be useful in your case as both the time and content values may vary.

So in other words, you will probably need to change your data model.

One possible approach would be to duplicate your data (quite common in NoSQL world) and have another array with only the ids of the cardAction, like:

cardActionIds = ["65....", "5789", ....]

This way you can use array-contains and once you have found the docs corresponding to the cardActionIds you manipulate the other array (i.e. cardAction)

Sign up to request clarification or add additional context in comments.

4 Comments

That's what I expected.. Would be nice if google could make this possible in the future. Do you have any ideas how I could change my data model to make it work? Thanks btw, for the answer!
Is there a specific reason why you put this data in an array apart from having a container for a collection of data? Do you use the array in the front end for example, displaying the entire list of cardsAction?
I've added a possible approach in my answer.
Thank you, will take a look at it. Cheers mate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.