Harika Putta
Harika Putta

Reputation: 63

Data search in Firestore

I have a Firebase Firestore in which the data is stored in Maps as in the picture below.

enter image description here

Now, I wanted to search for a string, let say "colour" in the entire document and retrieve the Map name, "COLOR" in this case. Likewise, if we search for "boy", it should return "BOLD".

var searchWord = "colour";
db.collection("keyWords").doc(window.user.uid).get().then((doc) => {
      console.log(doc.data());
      if (doc.exists === true) {
        //LOGIC HERE
    }}});

Can someone help me with this!

Upvotes: 0

Views: 294

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599081

So you want to find the map field in the document that contains a certain property?

let json = {
  BOLD: {
    Bone: 0,
    Phone: 1,
    boy: 2
  },
  COLOR: {
    Colour: 3,
    color: 4,
    colour: 5
  }
};

function find(prop) {
  Object.keys(json).forEach((key) => {
    if (json[key][prop]) {
      console.log(key);
    }
  })
}

find("boy") // prints "BOLD"

Upvotes: 1

Related Questions