Javascript 如何在mongodb中连接两个表并推入数组
这里我有两个集合Javascript 如何在mongodb中连接两个表并推入数组,javascript,mongodb,mongodb-query,Javascript,Mongodb,Mongodb Query,这里我有两个集合组织和组,我的要求是我要检查组织表中的oldOrgID,我必须获取schoolCode和school name,并进入主数据,直到我完成为止 现在我的问题是organization表我必须取orgID,我必须签入组表otherid.orgID,如果假设匹配意味着我必须取组表中的名称 组织: 小组: 我的代码 获取输出 预期产量 聚合代码 输出 您可以尝试聚合查询并使用 查找操作: 阅读更多: 你可以这样做: db.Organizations.aggregate([{ $lo
组织
和组
,我的要求是我要检查组织表中的oldOrgID
,我必须获取schoolCode
和school name
,并进入主数据
,直到我完成为止
现在我的问题是organization
表我必须取orgID
,我必须签入组表otherid.orgID
,如果假设匹配意味着我必须取组表中的名称
组织:
小组:
我的代码
获取输出
预期产量
聚合代码
输出
您可以尝试聚合查询并使用
查找操作:
阅读更多:
你可以这样做:
db.Organizations.aggregate([{
$lookup:
{
from: "Groups" // <collection to join>,
localField: "oldOrgID" // <field from the input documents>,
foreignField: "otherIds.orgId" // <field from the documents of the "from" collection>,
as: "group-section" <output array field>
}
}]);
db.Organizations.aggregate([{
$lookup:
{
发件人:“组”//,
localField:“oldOrgID”//,
foreignField:“otherid.orgId”//,
as:“集团部”
}
}]);
希望这能给您带来一些想法。$lookup是聚合查询的管道阶段之一,因此您需要将Mongo聚合查询用作:
db.collection.aggregate([
{
$match: {
oldOrgID: "176348"
}
},
{
$lookup: {
from: "other",
localField: "orgID",
foreignField: "otherIds.orgID",
as: "group-section"
}
},
{
$project: {
"group-section._id": 0,
"group-section.groupID": 0,
"group-section.otherIds": 0,
"group-section.version": 0
}
}
])
我在当前计算机中没有mongo设置。但是我尝试了所有我认为你放在上面的代码,它应该能工作。有人能更新我的代码吗,因为我是mongodb组织的新手
=orgID&组
=OtherID.orgIDI刚刚在我的上一个答案中更新了Mongo查询。请看一看,它可能会帮助你。我使用了你的代码,但没有工作。我得到的组部分
为空阵列我已更新了我的聚合代码
请根据我的预期答案验证并修改我的代码好@马约里库尔卡尼
var mainData = {};
var schoolCode = 176348 ;
db.Organizations.find({"oldOrgID" : schoolCode})
// .limit(1)
// .skip(15)
.forEach(function(doc){
var OrgID = doc.orgID;
if(mainData[OrgID] === undefined )
{
mainData[OrgID] = {}; // org name undefined means we are making empty object here
}
mainData[OrgID]['schoolCode'] = doc.oldOrgID;
mainData[OrgID]['schoolName'] = doc.name;
});
mainData
{
"5b6c82462fb9ca35444d0ba2" : {
"schoolCode" : "176348",
"schoolName" : "The Punjab Public School"
}
}
{
"5b6c82462fb9ca35444d0ba2" : {
"schoolCode" : "176348",
"schoolName" : "The Punjab Public School",
"group-section" :
[
{
"name" : "8 B"
},
{
"name" : "8 A"
}
]
}
}
db.Organizations.aggregate([
{
$match: {
oldOrgID: "176348"
}
},
{
$lookup: {
from: "Groups",
localField: "orgID",
foreignField: "otherIds.orgID",
as: "group-section"
}
},
{
$project: {
"group-section._id": 0,
"group-section.groupID": 0,
"group-section.otherIds": 0,
"group-section.version": 0
}
}
])
{
"_id" : ObjectId("5c11efebd9cb4d35f47d6bd0"),
"orgID" : "5b6c82462fb9ca35444d0ba2",
"name" : "The Punjab Public School",
"oldOrgID" : "176348",
"group-section" : [ ]
}
db.Organizations.aggregate([{
$lookup:
{
from: "Groups" // <collection to join>,
localField: "oldOrgID" // <field from the input documents>,
foreignField: "otherIds.orgId" // <field from the documents of the "from" collection>,
as: "group-section" <output array field>
}
}]);
db.collection.aggregate([
{
$match: {
oldOrgID: "176348"
}
},
{
$lookup: {
from: "other",
localField: "orgID",
foreignField: "otherIds.orgID",
as: "group-section"
}
},
{
$project: {
"group-section._id": 0,
"group-section.groupID": 0,
"group-section.otherIds": 0,
"group-section.version": 0
}
}
])