相关疑难解决方法(0)

如何在Cloud Firestore中使用逻辑OR执行复合查询?

来自文档:

您还可以链接多个where()方法以创建更具体的查询(逻辑AND).

我该如何执行OR查询?例:

  1. 给我所有字段statusopenOR的文档upcoming
  2. 给我所有文件的领域status == openORcreatedAt <= <somedatetime>

database firebase google-cloud-platform google-cloud-firestore

24
推荐指数
2
解决办法
6322
查看次数

Firestore搜索数组包含多个值

如果我有一个简单的集合,例如:

Fruits:
  Banana:
   title: "Banana"
   vitamins: ["potassium","B6","C"]
  Apple:
   title: "Apple"
   vitamins: ["A","B6","C"]
Run Code Online (Sandbox Code Playgroud)

如果我要搜索含有维生素B6的水果,则可以执行以下操作:

db.collection("Fruits").whereField("vitamins", arrayContains: "A").getDocuments() { (querySnapshot, err)  in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            for document in querySnapshot!.documents {
                print("\(document.documentID) => \(document.data())")
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

这样一来,我便可以看到我收藏的所有含有维生素A的水果。但是,我如何能够搜索含有多种维生素(例如维生素B6和C)的水果?我不能简单地搜索["B6","C"],因为那样会寻找数组,而不是独立的字符串。

在Cloud Firestore中甚至有可能吗?如果没有,还有其他替代方法吗?

firebase swift google-cloud-firestore

8
推荐指数
3
解决办法
6797
查看次数

Firestore - 我想动态地将 where 语句添加到我的集合中

技术:我正在使用 Angular 7、Firestore、GeoFireX。

我想要的结果:如果用户需要该查询,我想添加 .where 查询。例如下面我有四个 .wheres。有时我可能只需要两个,因此如果用户按部门和品牌搜索,我只想使用 if 语句添加两个(参见方法:basicQueryBuilder)

下面的代码有效,但不是动态的。

此外,来自 GeoFireX 的 this.geo.collection 是否可以像普通集合一样具有 .limit(20) ?

当前尝试:

  public getFirestoreJobs(queryType: string, limit: number, pageSize: number): Observable<any> {
    limit = limit + pageSize;

    if (queryType === 'geo') {
      const collection = this.geoQueryBuilder();
      const center = this.geo.point(51.5074, 0.1278);
      const radius = 20;

      return collection.within(center, radius, 'point');
    } else {
      const collection = this.basicQueryBuilder();

      return collection.valueChanges();
    }
  }

  public geoQueryBuilder(): any  {
    return this.geo.collection('jobs', ref => ref
      .where('sector', '==', 'teacher')
      .where('brand', …
Run Code Online (Sandbox Code Playgroud)

firebase geofire angular google-cloud-firestore geofirestore

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

使用 Firestore 和 Typescript 实现动态Where条件

我正在尝试使用Firestore Firebase 和TypeScript实现存储库模式。

代码:

import { firestore } from "firebase-admin";
import { ISearchCriteria } from './ISearchCriteria'

//export class selectQuery<T> {
//    constructor(private query: firestore.Query<T>) { }
//}

export class DBContext {
    static current: DBContext = new DBContext();
    private db: firestore.Firestore;

    private constructor() {
        this.db = firestore();
    }

    collection(collectionName: string): firestore.CollectionReference<firestore.DocumentData> {
        return this.db.collection(collectionName);
    }

    public async where<T extends object>(collectionName: string, searchCriteria: ISearchCriteria[]): Promise<T[]> {
        var snapShot = this.collection(collectionName);
        for (var i = 0; i < searchCriteria.length; i++) { …
Run Code Online (Sandbox Code Playgroud)

firebase typescript google-cloud-firestore

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

Firestore 上的条件 where 查询

我试过在这里实施解决方案:

Firestore 查询中的条件 where 子句

Firestore:多个条件 where 子句

但它们似乎不起作用(请参阅下面的代码示例)。Firestore 有什么改变吗?我在下面的代码中搞砸了什么吗?

提前致谢!

对于上下文,以下内容位于反应功能组件内的 useEffect 钩子中。但是,我不认为这是相关的,因为下面的工作示例(没有条件查询)工作正常。

基本示例 - 过滤硬编码 - 工作正常。过滤器已应用

const query = db.collection('todos')
    .where('userId', '==', userId)
    .where('status', '==', 'pending');

query.onSnapshot((res) => {
  const todos = [];
  res.forEach((todo) => {
    todos.push(todo.data());
  });
});
Run Code Online (Sandbox Code Playgroud)

不起作用 - 返回具有所有状态的结果。IF 块中的 where 查询尚未应用

const query = db.collection('todos').where('userId', '==', userId);

if (filter === 'complete') {
  query.where('status', '==', 'pending');
}
if (filter === 'complete') {
  query.where('status', '==', 'complete');
}
query.onSnapshot((res) => {
  const todos = [];
  res.forEach((todo) …
Run Code Online (Sandbox Code Playgroud)

javascript firebase google-cloud-firestore

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