48

I'm trying to run a simple query, where I search for a document that contains a value inside an object array.

For instance, look at my database structure:

firestore db

I want to run a query similar to this:

db.collection('identites').where("partyMembers", "array-contains", {name: "John Travolta"})

What is the correct way to achieve this, is it even possible with Firestore?

Thanks.

1
  • I wanna do the same but with passing the ID ,could u hlep Commented Sep 1, 2022 at 12:31

4 Answers 4

50

As Frank has explained in his answer it is not possible, with array-contains, to query for a specific property of an object stored in an array.

However, there is a possible workaround: it is actually possible to query for the entire object, as follows, in your case:

db.collection('identites')
  .where(
    "partyMembers",
    "array-contains",
    {id: "7LNK....", name: "John Travolta"}
  )

Maybe this approach will suit your needs (or maybe not....).

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

15 Comments

I've tried to query for an entire object (the exact example above) but without success. Are you sure it's possible ? can't find infos about on docs. Frank can you confirm if it's possible and if true provide an example of syntax please
@VictorDias I confirm that it works. You must have an array of objects (Firestore map type) and query with the exact and entire object. If you have problem with this approach, please create a new SO question with your exact code and database structure and add a comment below with a link to it. I'll be happy to have a look at it.
Hi @RenaduTamec, finally it works, the name of requested field was misspelled. Thank you for support, your response convince me to insist and find the solution.
@VictorDias Cool! Happy to hear that I could help you. You may upvote my answer if you think it helped you! Thanks
wonderful. works good on my use case.
|
32

The array-contains operations checks if an array, contains a specific (complete) value. It can't check if an array of objects, contains an item with a specific value for a property.

The only way to do your query, is to add an additional field to your document with just the value you want to query existence on. So for example: partyMemberNames: ["John Travolta", "Olivia Newton"].

1 Comment

Since you said that there is no way to run the query I mentioned, I"ll accept it as the answer, thank you.
0

If you control the doc structure and your queried term is unique and doesn't contain special characters, you could turn your array into a map:

partyMembers {
  "7LNKFB9ql": "John Travolta": 
}

and query with e.g. where('partyMembers.7LNKFB9ql', '!=', "")

Lots of ifs, but if dealing with unique user ids, could be an option. In the OPs example of querying by name ("John Travolta") and not id, this might be less feasible, but maybe for someone else.

Comments

-1

If you want to extract name: "John Travolta" from "partyMembers" array in a document. you can achieve this by some similar approach in which you can loop through all arrays in a document to find this name.

const [names, setNames] = React.useState([])

const readAllNames = async() => {
const snapshot = await firebase.firestore().collection('identites').doc(documentID).get()
      const filterData = snapshot.data().question.map(val => val.name === "John Travolta" ? val : null)
      setNames( filterData.filter(e=>e) );
}
This technique is used in perticular Document as we are giving .doc(documentID) This way you can get all the arrays having name: "John Travolta" in names constant.

Comments

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.