@Id field annotation for storing POJO in Firebase Firestore?

本秂侑毒 提交于 2021-01-29 11:13:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!