相关疑难解决方法(0)

Firestore:如何使用不等式/不等于执行查询

我想从Firestore集合中选择仅由我编写的文章.
这真的很难吗?

每篇文章都有字段"owner_uid".

多数民众赞成:
我只想写相当于"select * from articles where uid<>request.auth.uid"

firebase google-cloud-firestore

39
推荐指数
2
解决办法
2万
查看次数

如何在Cloud Firestore中查询不存在的文档密钥

假设我有一个带有一些可选属性的数据模型.这可以是例如具有"firstname","lastname"和可选的"website"属性的用户对象.

在Cloud Firestore中,只有具有已知网站的用户文档才会设置"网站"属性,对于所有其他用户文档,此属性将不存在.

我现在的问题是如何在没有 "网站"属性的情况下查询所有用户文档.

谢谢.

firebase google-cloud-firestore

11
推荐指数
1
解决办法
1万
查看次数

Firestore 查询角色?

文档中指出,您可以通过将字符串与另一个字符串值进行比较来进行查询,例如:

citiesRef.where('name', '>=', 'San Francisco');
citiesRef.where('state', '>=', 'CA').where('state', '<=', 'IN');
Run Code Online (Sandbox Code Playgroud)

然后文档中的角色部分展示了如何在 Firestore 文档中应用角色。然而,它没有显示如何查询这个..但如上面的示例所示,这应该像下面这样工作:

citiesRef.where(`roles.${user.uid}`, '>', '');
Run Code Online (Sandbox Code Playgroud)

所以上面的查询应该返回所有大于空字符串的值的文档,对吧?

在我的代码中,我有包含一个文档的组织集合:

{
  "name": "MyAmazingCompany",
  "roles": {
    "my-user-uid": "owner"
  }
}
Run Code Online (Sandbox Code Playgroud)

如果我尝试查询我担任以下角色的组织:

organizationsRef.where(`roles.${user.uid}`, '>', '');
Run Code Online (Sandbox Code Playgroud)

我将Uncaught Error in onSnapshot: Error: Missing or insufficient permissions.进入浏览器控制台(使用 firebase npm 包版本 5.1.0 并尝试使用 5.0.3)。

只是为了确保我应该有权访问该文档,测试了以下查询,它有效并且返回该组织。

organizationsRef.where(`roles.${user.uid}`, '==', 'owner');
Run Code Online (Sandbox Code Playgroud)

那么有什么问题吗?

还有人声称它应该有效:Firestore select where is not null

这是我的规则:

service cloud.firestore {
  match /databases/{database}/documents {
    function isSignedIn() {
      return request.auth != null;
    }

    function …
Run Code Online (Sandbox Code Playgroud)

javascript firebase google-cloud-firestore

7
推荐指数
1
解决办法
1252
查看次数

查询与空字段的比较

我的 Firestore 集合包含带有字符串字段的文档,该字段可以为null

我期待如果我查询:

Collection("products").Where("producedDate", "<", "2018-01-15")
Run Code Online (Sandbox Code Playgroud)

我会得到“producedDate”早于“2018-10-15”的所有产品,包括那些“producedDate”为空的产品。

但实际上我没有得到nulls。

这是故意的还是一个错误?

go firebase google-cloud-firestore

3
推荐指数
1
解决办法
2881
查看次数

firestore 安全规则 resource.data 为空对象

在 Firestore 安全规则中,resource.data 始终是一个空对象,这是错误还是什么?

我的firestore规则:

service cloud.firestore {
  match /databases/{database}/documents {
    match /hospitals/{document=**}{

      // allow read :if resource.data.size() == 0; //this return true, resource.data is an empty object

          allow read :if resource.data.name != null; // this doesn't work
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我的javascript:

auth().onAuthStateChanged((user) => { 
  if (user) {

    //db is the firestore instance
    db.collection('/hospitals').get()
      .then(printResult)

  } else {
    
  }
}) 
Run Code Online (Sandbox Code Playgroud)

这是我当前的数据库快照 图片

解决了 :

感谢弗兰克的回答

在我的情况下,当我们查询多个文档时,问题依赖于 firestore 安全性不会评估实际文档值

//this doesn't firestore doesnt' evaluate the documetn
db.collection('hospitals').get()

//this will work ,if you …
Run Code Online (Sandbox Code Playgroud)

javascript firebase-security google-cloud-firestore

3
推荐指数
1
解决办法
2266
查看次数

Firestore选择字段为空的位置

如何选择没有特定字段的所有文档?我有以下结构:

user = {
    firstName: 'blabla',
    lastName: 'bloblo',
    address: {
        city: 'xxxx',
        state: 'xxx'
    }
}
Run Code Online (Sandbox Code Playgroud)

我曾尝试将其与null进行比较,但未成功。

let db = firebase.firestore();  
let querySnapshot = await db
    .collection("users")
    .where("address", "==", null)
    .get();
Run Code Online (Sandbox Code Playgroud)

javascript node.js firebase google-cloud-firestore

1
推荐指数
1
解决办法
4502
查看次数