Setting custom key when pushing new data to firebase database

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

Setting custom key when pushing new data to firebase database



Well, I am new to Firebase and I want to have my own keys while pushing new data to database.



Problem:


FireBase.push().setValue(mapped_values);



This gives structure like below:



 database output



How can I create my own custom key there?
Such as username or something.




6 Answers
6



Calling push() will generate a key for you.


push()



If instead you use child(), you can determine they key/path yourself.


child()


ref.child("Victor").setValue("setting custom key when pushing new data to firebase database");





why is this not marked as answer...??
– Lakshman Pilaka
Nov 29 '16 at 20:42





because Victor Davis doesn't care
– user3734429
Dec 16 '16 at 17:14





Deserves to be marked...
– Angad Singh
Jan 26 '17 at 17:25





@FrenchyNYC Not if you use .child('customkey')...
– Bojan Kogoj
Mar 27 '17 at 11:43


.child('customkey')...





@trojan setting in the constructor: constructor(private db: AngularFireDatabase) you can do: this.db.database.ref().child('somechild').set('somevalue')
– Diego
Jun 15 '17 at 7:49


String key="1234567sdfsf8";
//custom object
User user=new User();
DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("Users").child(key).setValue(user);





life saver. thank you very much
– shinta
Oct 1 '17 at 3:57



You can create a custom key using setValue() even if the root contains many children
for example if 'Users' is the root and you want to add users with email as a key it will be like this


firebase.child("firebase url").child("Users").child("user_1 email").setValue(...)

firebase.child("firebase url").child("Users").child("user_2 email").setValue(...)



etc



Hope this helps.



If you are using FirebaseUI :


private static final CollectionReference usersCollection = FirebaseFirestore.getInstance().collection("users");

User user = new User("MyUsername", "MyPictureUrl");
String userKey = "1234567sdfsf8";

usersCollection.document(userKey).set(user); //MAGIC LINE



Just for sharing the knowledge.



if you are using fire-sharp, you can create the custom key as follows


IFirebaseConfig config = new FirebaseConfig

AuthSecret = "SecretKey",
BasePath = "https://abc.firebaseio.com/",
Host = "abc.firebaseio.com/"
;
IFirebaseClient client = new FirebaseClient(config);

var obj = new Users

FirstName = "test",
MiddleName = "user",
LastName = "xyz"

;

SetResponse response = client.SetAsync("Profile", "YourID");//you can use Set() as well
response = client.SetAsync("Profile/YourID", obj);//you can use Set() as well



Simple and Fast


Map<String,Object> taskMap = new HashMap<>();
taskMap.put("Name", name.getText().toString());
taskMap.put("km", km.getText().toString());
// taskMap.put("age", "45");
taskMap.put("Day", day.getText().toString());
mDatabaseReference.push().setValue(taskMap);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

Executable numpy error

Trying to Print Gridster Items to PDF without overlapping contents

Mass disable jenkins jobs