Reputation: 7143
According to firestore docs, I can perform the equivalent of a '!=' query by combining '>' and '<' queries:
Queries with a != clause. In this case, you should split the query into a greater-than query and a less-than query. For example, although the query clause where("age", "!=", "30") is not supported, you can get the same result set by combining two queries, one with the clause where("age", "<", "30") and one with the clause where("age", ">", 30).
But how exactly do I do that? If possible, please provide the code for that exact example (query for result != 30).
Upvotes: 1
Views: 633
Reputation: 83153
Actually, using a query like the following one will not work (based on the example on cities, found in the documentation you refer to):
var query = citiesRef.where("population", ">", 860000).where("population", "<", 860000);
I think the documentation excerpt you refer to means that you have to declare two queries (see below) and combine, in your code, the results of these two queries.
var query1 = citiesRef.where("population", ">", 860000);
var query2 = citiesRef.where("population", "<", 860000);
or
var query1 = yourRef.where("age", ">", "30");
var query2 = yourRef.where("age", "<", "30");
Here is a code that works for the cities example from the doc. Open these two HTML pages one after the other. The first one will create some cities records. The second one will concatenate the results of the two queries in one array and print it in the console.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>2</title>
<script src="https://www.gstatic.com/firebasejs/5.3.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.3.1/firebase-firestore.js"></script>
</head>
<body>
<script>
// Initialize Firebase
var config = {
apiKey: ".....",
authDomain: ".....",
databaseURL: ".....",
projectId: "....."
};
firebase.initializeApp(config);
var db = firebase.firestore();
var citiesRef = db.collection('cities');
citiesRef.doc("SF").set({
name: "San Francisco", state: "CA", country: "USA",
capital: false, population: 860000,
regions: ["west_coast", "norcal"] });
citiesRef.doc("LA").set({
name: "Los Angeles", state: "CA", country: "USA",
capital: false, population: 3900000,
regions: ["west_coast", "socal"] });
citiesRef.doc("DC").set({
name: "Washington, D.C.", state: null, country: "USA",
capital: true, population: 680000,
regions: ["east_coast"] });
citiesRef.doc("TOK").set({
name: "Tokyo", state: null, country: "Japan",
capital: true, population: 9000000,
regions: ["kanto", "honshu"] });
citiesRef.doc("BJ").set({
name: "Beijing", state: null, country: "China",
capital: true, population: 21500000,
regions: ["jingjinji", "hebei"] });
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>2</title>
<script src="https://www.gstatic.com/firebasejs/5.3.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.3.1/firebase-firestore.js"></script>
</head>
<body>
<script>
// Initialize Firebase
var config = {
apiKey: ".....",
authDomain: ".....",
databaseURL: ".....",
projectId: "....."
};
firebase.initializeApp(config);
var db = firebase.firestore();
var citiesRef = db.collection('cities');
var query1 = citiesRef.where("population", ">", 860000);
var query2 = citiesRef.where("population", "<", 860000);
var fullArray = [];
query1.get()
.then(function (querySnapshot) {
console.log(querySnapshot.docs);
(querySnapshot.docs).forEach((element, index, array) => {
console.log(element.data().population);
});
fullArray = fullArray.concat(querySnapshot.docs);
return query2.get();
})
.then(function (querySnapshot) {
console.log(querySnapshot.docs);
(querySnapshot.docs).forEach((element, index, array) => {
console.log(element.data().population);
});
fullArray = fullArray.concat(querySnapshot.docs);
console.log('Final resulting array:');
fullArray.forEach((element, index, array) => {
console.log(element.data().population);
});
})
.catch(function (error) {
console.log("Error getting documents: ", error);
});
</script>
</body>
</html>
Upvotes: 2
Reputation: 1384
You question, can found the answer on the official document.
https://firebase.google.com/docs/firestore/query-data/queries
Query limitations
Queries with a != clause. In this case, you should split the query into a greater-than query and a less-than query. For example, although the query clause where("age", "!=", "30") is not supported, you can get the same result set by combining two queries, one with the clause where("age", "<", "30") and one with the clause where("age", ">", 30).
Upvotes: 0