问题
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:

How can I create my own custom key there? Such as username or something.
回答1:
Calling push()
will generate a key for you.
If instead you use child()
, you can determine they key/path yourself.
ref.child("Victor").setValue("setting custom key when pushing new data to firebase database");
回答2:
String key="1234567sdfsf8";
//custom object
User user=new User();
DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("Users").child(key).setValue(user);
回答3:
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.
回答4:
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
回答5:
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
回答6:
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);
回答7:
if database reference child is fixed string, new value will not add. just it will update the previous value. for example :
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference();
String mText = // get data from editText or set your own custom data
now if I insert data like this:
myRef.child("abc").child("cba").setValue(mText);
every time I insert data it will update my previous data. It will not add new data. Because my reference is fixed here(myRef.child("abc").child("cba")
// this one, which is always fixed).
Now change the the value of child "cba" to a random or dynamic value which will not fix. For example:
Random rand = new Random();
// Obtain a number between [0 - 49].
int n = rand.nextInt(50);
myRef.child("abc").child(String.valueOf(n)).setValue(mText);
In this case it will add a new value instead of updating. because this time reference is not fixed here. it is dynamic. push() method exactly do the same thing. it generates random key to maintain unique reference.
回答8:
In POST request it will generate ID's but in PATCH, PUT request it will mention the key which will be provided by you.
回答9:
As an update to the top answer, the Firebase API has been changed and setValue()
does not work anymore. Now you must use the set()
function instead:
ref.child("Victor").set("setting custom key when pushing new data to firebase database");
来源:https://stackoverflow.com/questions/37483406/setting-custom-key-when-pushing-new-data-to-firebase-database