admin管理员组文章数量:1336633
How to add a custom id to a firestore document instead of automatically generated id by firebase 9?
i've problem transforming this code into firebase 9 version -
db.collection("cities").doc("LA").set({
name: "Los Angeles",
state: "CA",
country: "USA" })
How to add a custom id to a firestore document instead of automatically generated id by firebase 9?
i've problem transforming this code into firebase 9 version -
db.collection("cities").doc("LA").set({
name: "Los Angeles",
state: "CA",
country: "USA" })
Share
Improve this question
edited Dec 18, 2021 at 14:25
Mahmudul Ahsan
asked Dec 18, 2021 at 14:17
Mahmudul AhsanMahmudul Ahsan
431 silver badge6 bronze badges
1
- thanks bro got it. I'm newbie at firebase, didn't notice that tab. – Mahmudul Ahsan Commented Dec 18, 2021 at 14:29
2 Answers
Reset to default 8The example that you've provided seems to be from Firestore's documentation itself. Switch to (modular
) tab instead of (namespaced
) to see the same in V9 syntax.
The syntax for setting a document with custom ID using Modular SDK is follows (for the same example):
import { doc, setDoc } from "firebase/firestore";
await setDoc(doc(db, "cities", "LA"), {
name: "Los Angeles",
state: "CA",
country: "USA"
});
after researching a lot of time I got a solution for this by myself
Try this one.
So, the solution is:
FirebaseFirestore db = FirebaseFirestore.getInstance();
// [START set_document]
Map<String, Object> city = new HashMap<>();
city.put("batch", "25");
city.put("blood", ""+stuBlood.getText().toString());
city.put("email", ""+stuEmail.getText().toString());
city.put("facebook", ""+stuFacebook.getText().toString());
city.put("gender", ""+stuGender.getText().toString());
city.put("id", ""+stuID.getText().toString());
city.put("image", ""+stuImage.getText().toString());
city.put("location", ""+stuLocation.getText().toString());
city.put("name", ""+stuName.getText().toString());
city.put("phone", ""+stuPhone.getText().toString());
city.put("whatsapp", ""+stuWhatsApp.getText().toString());
city.put("telegram", ""+stuTelegram.getText().toString());
city.put("session", "2020-2021");
String documentID = "YOUR_CUSTOM_DOC_ID (You can also add String here)";
db.collection("YOUR_COLLECTION_PATH").document(""+documentID)
.set(city)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "DocumentSnapshot successfully written!");
Toast.makeText(add_student_info.this, "Added Successfully!", Toast.LENGTH_LONG).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error writing document", e);
Toast.makeText(add_student_info.this, "Failed to add Student data!", Toast.LENGTH_LONG).show();
}
});
// [END set_document]
}
Hope it's working now
本文标签: javascriptHow to add Document with Custom ID to firestore in firestore 9Stack Overflow
版权声明:本文标题:javascript - How to add Document with Custom ID to firestore in firestore 9 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742387432a2465312.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论