根据火药库文档,我可以通过组合‘’>和'<‘查询来执行相当于'!=’‘的查询:
带有!=子句的查询。在这种情况下,您应该将查询分为大于查询和小于查询。例如,虽然不支持查询子句where("age“、"!=”、"30"),但可以通过合并两个查询来获得相同的结果集,一个查询与其中的子句(“age”、"<“、"30")结合在一起,另一个查询的子句为”age“、">”、30)。
但我该怎么做呢?如果可能,请提供该示例的代码(查询结果!= 30)。
发布于 2018-08-21 08:28:58
实际上,使用下面这样的查询是行不通的(基于您参考的文档中关于城市的示例):
var query = citiesRef.where("population", ">", 860000).where("population", "<", 860000);我认为您提到的文档摘录意味着您必须声明两个查询(参见下面),并在代码中结合这两个查询的结果。
var query1 = citiesRef.where("population", ">", 860000);
var query2 = citiesRef.where("population", "<", 860000);或
var query1 = yourRef.where("age", ">", "30");
var query2 = yourRef.where("age", "<", "30");下面是一个用于文档中的城市示例的代码。一个接着一个打开这两个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');
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>发布于 2018-08-21 08:42:29
你的问题,能在公文上找到答案。
https://firebase.google.com/docs/firestore/query-data/queries
查询限制
带有!=子句的查询。在这种情况下,您应该将查询分为大于查询和小于查询。例如,虽然不支持查询子句where("age“、"!=”、"30"),但可以通过合并两个查询来获得相同的结果集,一个查询与其中的子句(“age”、"<“、"30")结合在一起,另一个查询的子句为”age“、">”、30)。
https://stackoverflow.com/questions/51944108
复制相似问题