I am new to firestore. I want to get the name from a different collection by using join query by ID. How can I do that in firestore?
Here is some sample collection.
I have two collection.Employee and department.
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
I want to query employee collection and wants to add dept document as part of the response.
Here is sample response I am trying to get.
{
"empid": 2001,
"empname" : Jon
"Dept" :{
"Id" :1001,
"DeptName" : HR
}
}
Here is my sample code to get employee data.
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.' })
});
}
How to add dept object to this employee?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…