To save a restaurant as a favorite, you need to create a Map
and not an array beneath each user document. According to official documentation:
Although Cloud Firestore can store arrays, it does not support querying array members or updating single array elements.
So your database structure should look like this:
Firestore-root
|
--- users
|
--- uid
|
--- // user details
|
--- favoriteRestaurants
|
--- restaruantIdOne: true
|
--- restaruantIdTwo: true
In code, should look like this:
Map<String, Object> favoriteRestaurants = new HashMap<>();
Map<String, Object> restaurant = new HashMap<>();
restaurant.put("restaruantIdOne", true);
favoriteRestaurants.put("favoriteRestaurants", restaurant);
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
DocumentReference uidRef = rootRef.collection("users").document(uid);
uidRef.update(favoriteRestaurants);
If you want to remove that a restaurant from favorites, please use the following code:
Map<String, Object> restaurant = new HashMap<>();
restaurant.put("favoriteRestaurants.restaruantIdOne", FieldValue.delete());
uidRef.update(restaurant);