Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – Firestore conditional array query

Posted on November 11, 2022 By

Solution 1 :

You can’t query individual array elements in Firestore. You can only check whether an array contains a specific item.

It sounds like your array items have a specific meaning, in which case you should create (nested) fields that indicate/name the roles. For example:

participants: {
  creator: "uid1",
  receiver: "uid2"
}

With that you can then query the nested fields with dot notation:

.where("participants.creator", "!=", "uid1")
.where("participants.receiver", "==", null)

Keep in mind there that the participants.receiver field still has to exist in the latter case and have a value of null. Firestore can’t filter in fields that don’t exist.

Problem :

I am trying to get all documents where the length of the “users” array is less than 2 and where the userId is not present already. I am doing the following query, but it is not executing correctly. What is the problem and how can I fix it? I just want all documents where there is only one entry in the array “users” and where the array does NOT contain the current userId.

await FirebaseFirestore.instance
  .collection('rooms')
  .where("users"[0], isNotEqualTo: userId)
  .where('users'[1], isEqualTo: null)
  .get()
  .then((snapshot) async {
// If no empty room is found, create room
if (snapshot.docs.isEmpty) {
  print("No empty room found, creating new room");
  roomId = await createRoom(userId);
  return roomId;
}

Comments

Comment posted by cipano

Thanks so much! Perfectly answered my question! Just one follow-up question: I am adding the participants in my room in the code like: “users”: [userId1] – How do I add the nested field when doing the firestore query?

Comment posted by firebase.google.com/docs/firestore/manage-data/…

Adding a nested field, is done by passing the first snippet I have in my answer. Querying and updating a nested field is then done with dot syntax. Also see

READ  [FIXED] java - unable to post array into server using retrofit
Powered by Inline Related Posts

Android Tags:android, dart, flutter, google-cloud-firestore

Post navigation

Previous Post: [FIXED] android developer api – there is a ‘Late Initialization Error’ during flutter development
Next Post: [FIXED] android – How to set SSL pinning using retrofit for Firebase Realtime Database Url?

Related Posts

[FIXED] java – Render arrow with direction between 2 anchors ARCore Android
[FIXED] c# – is there any way to allow user to enter only characters and allow whitespace in entry field on keypress in Xamrin Forms Android
[FIXED] java – MPAndroidChart I’d like to change the color of the graph at the top of the limit line Android
[FIXED] Android – SharedPreferences’s getString prints the default value Android
[FIXED] android – How can a Delagetes.observable in a BroadcastReceiver be unit tested? Android
[FIXED] Databiding Unresolve reference after update android studio 3.6 Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What color are dead flea eggs?
  • What is Indiana vine?
  • What’s the downside of a Chromebook?
  • Is phosphide the same as phosphorus?
  • Why do you need an S bend?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme