我已经构建了这个复合的whereField查询,但是由于某种原因,它似乎没有应用任何过滤器。我使用searchCriteria字段将动态数据传递到whereFields中。这是正确的更改,但是无论组合如何,我总是会取回所有记录。知道发生了什么吗?
let dbCollection = db.collection(Constants.FirebaseFirestore.COLLECTION_ITEM)
dbCollection.whereField(Constants.FirebaseFirestore.ITEM_OWNER_FIELD, isEqualTo: searchCriteria.owner)
dbCollection.whereField(Constants.FirebaseFirestore.ITEM_CATEGORY_FIELD, isEqualTo: searchCriteria.category)
dbCollection.order(by: Constants.FirebaseFirestore.ITEM_DATE_CREATED_FIELD, descending: false)
数据
category "need" (string)
count 10
date_created 1586663033.023617
name "Needs 1"
owner "INvSUJyBwecZgZg0o33dLu3VgGt2"
category "stock" (string)
count 10
date_created 1586664033.023617
name "Stock 1"
owner "INvSUJyBwecZgZg0o33dLu3VgGt2"
答案 0 :(得分:1)
在Query上调用whereField不会更改您调用它的Query对象。它将应用过滤器创建一个新的Query对象。因此,您应该按照documentation中显示将它们链接在一起的模式进行操作:
let query = db
.collection(Constants.FirebaseFirestore.COLLECTION_ITEM)
.whereField(Constants.FirebaseFirestore.ITEM_OWNER_FIELD, isEqualTo: searchCriteria.owner)
.whereField(Constants.FirebaseFirestore.ITEM_CATEGORY_FIELD, isEqualTo: searchCriteria.category)
.order(by: Constants.FirebaseFirestore.ITEM_DATE_CREATED_FIELD, descending: false)
或者每次调用时都要记住新的查询对象:
var query = db.collection(Constants.FirebaseFirestore.COLLECTION_ITEM)
query = query.whereField(Constants.FirebaseFirestore.ITEM_OWNER_FIELD, isEqualTo: searchCriteria.owner)
query = query.whereField(Constants.FirebaseFirestore.ITEM_CATEGORY_FIELD, isEqualTo: searchCriteria.category)
query = query.order(by: Constants.FirebaseFirestore.ITEM_DATE_CREATED_FIELD, descending: false)