Firestore: how to perform a query with inequality / not equals

后端 未结 7 1751
南笙
南笙 2020-11-22 05:05

I want select from Firestore collection just articles written NOT by me.
Is it really so hard?

Every article has field "owner_uid".

7条回答
  •  青春惊慌失措
    2020-11-22 05:55

    This is an example of how I solved the problem in JavaScript:

    let articlesToDisplay = await db
      .collection('articles')
      .get()
      .then((snapshot) => {
        let notMyArticles = snapshot.docs.filter( (article) => 
          article.data().owner_uid !== request.auth.uid
        )
        return notMyArticles
      })
    

    It fetches all documents and uses Array.prototype.filter() to filter out the ones you don't want. This can be run server-side or client-side.

提交回复
热议问题