问题
We have a complex JSON having multiple sub-objects inside main object. For example:
dept: {
name: Dept1,
emps: [
{
empCode: 001,
empName: Emp001
}
]
}
If we use db.collection("dept").add(deptPojo) then it auto creates "emps" subcollection and auto assigns document id for each emp object.
But we want empCode to be assigned as document id for each emp document.
Is this achievable? How? Is there any @Id annotation in firebase firestore api?
回答1:
There is currently no way to indicate the ID using an annotation, but I believe the Firebase team is looking into a solution for that.
回答2:
Sorry guys, my bad!
I was under impression that array of emps inside dept in my example above will end up with creating sub-collection emps having each entry in an array an auto-generated document id.
But later I understood that, I was wrong in this assumption. The behavior is as follows.
If you have nested objects (i.e. parent having a field of type List or Map. Then they are stored as an array in firestore instead of creating a dedicated sub-collection.
Means in case of saving pojo object, firebase doe's not auto create sub-collections. As show in screenshot here... devices is a Map of device objects but it stored as array.
Thanks.
来源:https://stackoverflow.com/questions/56981508/id-field-annotation-for-storing-pojo-in-firebase-firestore