firebase Cloud firestore '!='查询

wwtsj6pe  于 2023-04-13  发布在  其他
关注(0)|答案(2)|浏览(156)

根据firestore docs,我可以通过组合'〉'和'〈'查询来执行'!='查询的等价物:
带!=子句的查询。在这种情况下,应该将查询拆分为大于查询和小于查询。例如,尽管查询子句where(“age”,“!=",“30”)不受支持,您可以通过组合两个查询来获得相同的结果集,其中一个查询带有where子句(“age”,“〈",“30”)和一个具有子句where(“age”,“〉",30)。
但是我到底该怎么做呢?如果可能的话,请提供这个例子的代码(查询结果!= 30)。

bxfogqkk

bxfogqkk1#

实际上,使用像下面这样的查询是不起作用的(基于城市的示例,可以在您参考的文档中找到):

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中的cities示例。依次打开这两个HTML页面。第一个页面将创建一些cities记录。第二个页面将把两个查询的结果连接到一个数组中,并在控制台中打印出来。

<!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>
7dl7o3gd

7dl7o3gd2#

你的问题,可以在公文上找到答案。
https://firebase.google.com/docs/firestore/query-data/queries

查询限制

带!=子句的查询。在这种情况下,应该将查询拆分为大于查询和小于查询。例如,尽管查询子句where(“age”,“!=",“30”)不受支持,您可以通过组合两个查询来获得相同的结果集,其中一个查询带有where子句(“age”,“〈",“30”)和一个具有子句where(“age”,“〉",30)。

相关问题