Find object in Firebase by one value - firebase

I have next database list (usernames + user id).
How can i find object by user id and change his key (username)?
I'm using AngularFire2 with Angular 5.

You can find a child node by its value with a query like this:
var users = firebase.database().reference("usernames");
var query = users.orderByValue().equalTo("Sk6I..."ltA2");
By attaching a listener to this query you'll be able to find the reference and the key of the user (or "any users", since technically there may be more keys with the same value) matching the UID.
But you can't rename a node. You'll have to remove the existing node, and create a new one. For more on this see:
Firebase: Update key?
Firebase API for moving a tree branch from a collection to another one
Is it possible to rename a key in the Firebase Realtime Database?

Solution for my case:
this.db.list('usernames', ref => ref.equalTo(uid)).remove() // Remove old value
this.db.list('usernames', ref => ref.equalTo(uid)).set(
username, uid
) // Create new value

Related

How to add an object to a list in Firebase realtime database without downloading the entire list beforehand?

I am using Firebase and I have a list of elements called usernames.
The usernames contain a key: value (username: userid). Is there a way to add an item to the list without that the client downloads the entire username list?
For example, I am using db.list(..) but I can't find an info whether Firebase downloads all usernames before appending my data.
method
push(value: T)
Creates a new record on the list, using the Realtime Database's push-ids.
update(keyRefOrSnap: string, value: T)
Firebase
Updates an existing item in the array. Accepts a key, database reference, or an unwrapped snapshot.
https://github.com/angular/angularfire/blob/master/docs/rtdb/lists.md#api-summary
You can get a direct reference to that username node and set the UID:
firebase.database().ref('usernames/' + username).set(userUID)

Compare 'currentUser?.uid' To childByAutoIds

Using firebase and Swift. Is there any way of comparing the current user:
'let uid = Auth.auth().currentUser?.uid'
with the autogenerated id's under 'Users' or 'Advertiser' in the Firebase Structure?
I want to do a If statement and see if the currentUser matches with the Id's under Advertiser or Users.
If you're trying to check if a certain node exists in the database, that should be a fairly simple with something like:
Database.database().reference(withPath: "Advertiser").child(uid)
.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() ...
Also see:
Swift Firebase Check if user exists

Firestore: Updating field in an object removes previous field

I am using Flutter cloud firestore. Here is how my database looks like.
I want to add more fields (like "2222") in a_numbers object. I use updateData()like this,
DocumentReference ref = Firestore.instance.document("products/-LMhR5cAyW4T0sa03UtU");
ref.updateData({"a_numbers": {"2222" : false}});
The above snippet basically deletes the previous value (1111) and then updates the database with 2222 field.
Any solution?
To update 'a_numbers' with Pair without removing the previous values, you should assign the reference path (example: 'a_number.2222') to K and assign value to V. I edited your code. please review it for more information.
DocumentReference ref = Firestore.instance.document("products/-LMhR5cAyW4T0sa03UtU");
ref.updateData({'a_numbers.2222': value});
This is the expected behavior, in your Flutter application, you need to store a_numbers in a Map and add any new (key,value) pairs using myMap.addAll({key:value}), then you can use ref.updateData(myMap)

How can i generate custom IDs for objects in firebase?

Please I want to give custom IDs to my database objects in Firebase but I don't now how to do it. Firebase creates default IDs for database objects which I don't want. I want to be able to assign my own IDs to objects or the child nodes of in the database for unique identification.
Most likely you're adding the items to the database with something like:
ref.push().set("my value");
This generates a new unique key under ref and sets your value on it.
If you want to use you own key/name for the child location, add the item with:
ref.child("my key").set("my value");
You cannot customize ID of firebase object, but you can create another field with ID role.
ref.child("my_id").set("customize_id");
after that, using "Filter by key" to get exactly your object you want.
In our case: We need to have a user_id type Int and auto-increase, so we can't use default _id of firebase object, we create user_id ourself to solve this problem.

How do i add children to Firebase player UID?

I tried to add children nodes to a player with UID. I used child("node_1"). It creates only one node. How do I add items to this node?
To solve this, you need to have a unique identifier every time you try to add data to your database. Because a Firebase database is a NoSQL database and is structured as pairs of key and values, every node is a Map, which means in the case of Map, it replaces the old value with the new one.
Fortunately Firebase provides a method named push(). You can use it like this:
yourRef.push().child(name).setValue("John");
Which one you use add child node? Web, Ios or Android?
Alex Mamo was right: "you need to have a unique identifier every time you try to add data to your database"
Using for Web:
let node = firebase.database().ref().child(Your node)
var key= node.push().key // Get a key for a new
node.child(key).set(new value)
https://firebase.google.com/docs/database/web/read-and-write?authuser=0
Using for IOS:
let key = ref.child("posts").childByAutoId().key
https://firebase.google.com/docs/database/ios/read-and-write?authuser=0
And for Android:
String key = mDatabase.child("posts").push().getKey()
https://firebase.google.com/docs/database/android/read-and-write?authuser=0
Hope to help you.

Resources