firestore: query data

Perform Simple and Compound Queries in Cloud Firestore

Cloud Firestore provides powerful query functionality for specifying which documents you want to retrieve from a collection. These queries can also be used with either get() or addSnapshotListener(), as described in Get Data and Get Realtime Updates.

Note:  While the code samples cover multiple languages, the text explaining the samples refers to the Web method names.

Example data

To get started, write some data about cities so we can look at different ways to read it back:

WEB
SWIFT
OBJECTIVE-C
ANDROID
JAVA
PYTHON
MORE
 
  
FIRCollectionReference * citiesRef = [ self . db collectionWithPath :@ "cities" ];
[[ citiesRef documentWithPath :@ "SF" ] setData :@{
 
@ "name" : @ "San Francisco" ,
 
@ "state" : @ "CA" ,
 
@ "country" : @ "USA" ,
 
@ "capital" : @( NO ),
 
@ "population" : @ 860000
}];
[[ citiesRef documentWithPath :@ "LA" ] setData :@{
 
@ "name" : @ "Los Angeles" ,
 
@ "state" : @ "CA" ,
 
@ "country" : @ "USA" ,
 
@ "capital" : @( NO ),
 
@ "population" : @ 3900000
}];
[[ citiesRef documentWithPath :@ "DC" ] setData :@{
 
@ "name" : @ "Washington D.C." ,
 
@ "country" : @ "USA" ,
 
@ "capital" : @( YES ),
 
@ "population" : @ 680000
}];
[[ citiesRef documentWithPath :@ "TOK" ] setData :@{
 
@ "name" : @ "Tokyo" ,
 
@ "country" : @ "Japan" ,
 
@ "capital" : @( YES ),
 
@ "population" : @ 9000000
}];
[[ citiesRef documentWithPath :@ "BJ" ] setData :@{
 
@ "name" : @ "Beijing" ,
 
@ "country" : @ "China" ,
 
@ "capital" : @( YES ),
 
@ "population" : @ 21500000
}];
 

Simple queries

The following query returns all cities with state CA:

WEB
SWIFT
OBJECTIVE-C
ANDROID
JAVA
PYTHON
MORE
 
  
// Create a reference to the cities collection
FIRCollectionReference * citiesRef = [ self . db collectionWithPath :@ "cities" ];
// Create a query against the collection.
FIRQuery * query = [ citiesRef queryWhereField :@ "state" isEqualTo :@ "CA" ];
 

The following query returns all the capital cities:

WEB
SWIFT
OBJECTIVE-C
ANDROID
JAVA
PYTHON
MORE
 
  
FIRQuery * capitalCities =
   
[[ self . db collectionWithPath :@ "cities" ] queryWhereField :@ "capital" isEqualTo : @YES ];
 

The where() method takes three parameters: a field to filter on, a comparison operation, and a value. The comparison can be <<===>, or >=. For iOS, Android, and Java, the comparison operator is explicitly named in the method.

Some example filters:

WEB
SWIFT
OBJECTIVE-C
ANDROID
JAVA
PYTHON
MORE
 
  
[ citiesRef queryWhereField :@ "state" isEqualTo :@ "CA" ];
[ citiesRef queryWhereField :@ "population" isLessThan :@ 100000 ];
[ citiesRef queryWhereField :@ "name" isGreaterThanOrEqualTo :@ "San Francisco" ];
 

Compound queries

You can also chain multiple where() methods to create more specific queries (logical AND). However, to combine the equality operator (==) with a range comparison (<<=>, or >=), make sure to create a custom index.

WEB
SWIFT
OBJECTIVE-C
ANDROID
JAVA
PYTHON
MORE
 
  
[[ citiesRef queryWhereField :@ "state" isEqualTo :@ "CO" ]
    queryWhereField
:@ "name" isGreaterThanOrEqualTo :@ "Denver" ];
[[ citiesRef queryWhereField :@ "state" isEqualTo :@ "CA" ]
    queryWhereField
:@ "population" isLessThan :@ 1000000 ];
 

Additionally, you can only perform range comparisons (<<=>>=) on a single field:

Valid: Range filters on only one field

WEB
SWIFT
OBJECTIVE-C
ANDROID
JAVA
PYTHON
MORE
 
  
[[ citiesRef queryWhereField :@ "state" isGreaterThanOrEqualTo :@ "CA" ]
    queryWhereField
:@ "state" isLessThanOrEqualTo :@ "IN" ];
[[ citiesRef queryWhereField :@ "state" isEqualTo :@ "CA" ]
    queryWhereField
:@ "population" isGreaterThan :@ 1000000 ];
 

Invalid: Range filters on different fields

WEB
SWIFT
OBJECTIVE-C
ANDROID
JAVA
PYTHON
MORE
 
  
[[ citiesRef queryWhereField :@ "state" isGreaterThanOrEqualTo :@ "CA" ]
    queryWhereField
:@ "population" isGreaterThan :@ 1000000 ];
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值