1
let info = "every doc that starts with input preferable where i can place a limit on"
firebase.firestore().collection("usernames").doc(info);

I want to make a search bar where I get all users depending on the value I type in. And I'm not sure how to do that exectly. basicaly I want to get all docs in the collection usernames that contain some input string

Ender_Bit
  • 43
  • 7
  • There is no way to get the documents where a field **contains** a specific substring. See https://stackoverflow.com/questions/62244283/wildcard-firebase-query/62244968#62244968 and more – Frank van Puffelen Apr 17 '21 at 14:44

1 Answers1

0

To start with, what you have done with the code above is create a document with an id of the string 'info'. Only one unique id could exist which would make querying unnecessary. In order to do a string search it would also be best to split the string into an array. II assume what you want to do is something like:

let info = "some string to go in document"
info = info.split(" ");

// Add a new document with a generated id.
firebase.firestore().collection("usernames").add({
    info: info
})
.catch((error) => {
    console.error("Error adding document: ", error);
});

Then you can query all documents in the collection to see if the info array contains a certain string word:

let check = "string"

firebase.firestore().collection("usernames").where('info', 'array-contains', check).get().then(function(querySnapshot) {
    querySnapshot.forEach(function(doc) {
        console.log(doc.id, " => ", doc.data());
    });
});

Hope this is helpful, its a difficult problem

Tom Walsh
  • 119
  • 2
  • 12