簡體   English   中英

Firestore查詢-連接兩個集合

[英]Firestore Query - Joining two collections

我是新來的消防員。 我想通過按ID使用聯接查詢來從其他集合中獲取名稱。 我該怎么辦?

這是一些樣本集合。

我有兩個集合。員工和部門。

Department collection:
 1001 --> DeptId : 1001
          DeptName : Account
 1002 -->  DeptId : 1002
           DeptName : HR

Employee collection

2001 --> empId : 2001
         DeptId :1001
         empName : Jon
2002 -->  empId : 2002
         DeptId : 1002
         empName : Steve

我想查詢員工集合,並想添加部門文檔作為響應的一部分。 這是我嘗試獲取的示例響應。

{
  "empid": 2001,
  "empname" : Jon
  "Dept" :{
    "Id" :1001,
    "DeptName" : HR
  } 
}

這是我獲取員工數據的示例代碼。

function getEmployee(req, res)
{
 var empId = req.query.empId;
var obj = admin.firestore().collection('employee').doc(empId);
  obj.get()
 .then(function(emp) {
   if (emp.exists) {       
       return res.status(200).send(JSON.stringify(emp.data()));
   } else {              
       return res.status(200).send('not found');
   }
})
.catch(function(error) {
   res.status(500).send('Error getting data.' })
});
}

如何向該員工添加部門對象?

Firestore沒有聯接查詢。 如果要合並兩個文檔中的數據,則必須分別查詢它們,然后根據兩個文檔中的數據來組合響應。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM