代码之家  ›  专栏  ›  技术社区  ›  AVAVT

云火商店'!='查询

  •  1
  • AVAVT  · 技术社区  · 6 年前

    firestore docs ,我可以表演相当于一个'!='通过组合'>'和'<'查询进行查询:

    但我该怎么做呢?如果可能,请提供该示例的代码(查询结果!=30)。

    2 回复  |  直到 6 年前
        1
  •  2
  •   Renaud Tarnec    6 年前

    实际上,使用如下查询将不起作用(基于您参考的文档中有关城市的示例):

    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>
    
        2
  •  0
  •   youDaily    6 年前

    https://firebase.google.com/docs/firestore/query-data/queries

    查询限制

    用a查询!=条款。在这种情况下,应该将查询拆分为大于查询和小于查询。例如,尽管 查询子句的位置(“age”,“!不支持“,”30“),您可以 通过组合两个查询(一个查询与子句)获得相同的结果集 其中(“age”、“<”、“30”)和一个带有子句where(“age”、“>”, 30页)。