实际上,使用如下查询将不起作用(基于您参考的文档中有关城市的示例):
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");
下面是一个适用于城市的代码示例
doc
. 依次打开这两个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>