admin管理员组文章数量:1386656
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?
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?
Share Improve this question asked Jun 29, 2018 at 23:59 user5863509user5863509 2651 gold badge5 silver badges16 bronze badges1 Answer
Reset to default 4Firestore has no join queries. If you want to bine the data from two documents, you will have to query them individually, then pose your response based on the data from both.
本文标签: javascriptFirestore QueryJoining two collectionsStack Overflow
版权声明:本文标题:javascript - Firestore Query - Joining two collections - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744512433a2609960.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论