Firebase Firestore - OR query

2020-01-24 09:29发布

How to get data by multiple values of one field? For example, I have database with posts and I want to query for all posts where blogId is 1 or 2, sorting by timestamp.

collection("posts").whereEqualTo("blogId", "1")
.whereEqualTo("blogId", 2).orderBy("timestamp", Query.Direction.DESCENDING).limit(50)

Code above is not working :(

How to achieve this? Regards :)

8条回答
混吃等死
2楼-- · 2020-01-24 10:11

Firestore has no OR queries. They way you'd do that is by running two queries and splicing them.

You could for example:

const search = ( key, value ) =>  collection( 'users' ).where( key, '==', value ).get().then( doc => doc.data() )

Promise.all( [ search( 'name', 'John' ), search( 'name', 'Johnny' ) ] )
.then( ( [ one, two ] ) => one.concat( two ) )
.then( allResults => dothings() )
查看更多
迷人小祖宗
3楼-- · 2020-01-24 10:13

Try this

collection("posts").start("blogId", "1")
.end("blogId", 2).orderBy("timestamp", Query.Direction.DESCENDING).limit(50)
查看更多
登录 后发表回答