简体   繁体   English

查询子集合的字段并在firestore android中获取父文档快照

[英]Querying subcollection's field and get parent DocumentSnapshot in firestore android

My requirement if a other user has saved my number on their mobile,i want to get his profile data.Now I am querying the mobile field,which is inside the contacts sub collection.when I make query I get document snapshot of the subcollection's document.But,I want to get Whole profile data of that user so,I need to get the parent snapshot.How can i achieve this or else do i have to change the structure of firestore?我的要求,如果其他用户在他们的手机上保存了我的号码,我想获取他的个人资料数据。现在我正在查询联系人子集合内的移动字段。当我进行查询时,我得到子集合文档的文档快照。但是,我想获得该用户的完整个人资料数据,所以,我需要获得父快照。我怎样才能做到这一点,否则我必须改变 firestore 的结构? Thanks in advance提前致谢

[ [这就是我的 Firestore 结构的外观1

db.collectionGroup("contacts")
        .whereEqualTo("mobile",preference.getMobile()?.number)..get().addOnSuccessListener { snapShots ->
        val list = ArrayList<UserProfile>()
        for (s in snapShots) {
               //here i'm getting the contact data
           val contact = s.toObject(ModelMobile::class.java)
              //My need is getting userProfile Data
            /* val contact = s.toObject(UserProfile::class.java)
             list.add(contact)")*/
        }
    }

After the query on the contacts subcollection you will need to make another query for the parent document.在对contacts 子集合进行查询之后,您将需要对父文档进行另一个查询。 It's not possible to get them both at the same time in a single query.在单个查询中同时获取它们是不可能的。

for (s in snapShots) {
    var parentDocRef = s.reference.parent.parent
    // Now you have to get() the parentDocRef to get its contents
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM