Javascript mongoDB中的字段名别名

Javascript mongoDB中的字段名别名,javascript,json,node.js,mongodb,sails.js,Javascript,Json,Node.js,Mongodb,Sails.js,我正在使用mongoDB和newtonative命令,我喜欢使用别名作为字段名 这里我使用两个表(Company和employee),在Company表中,employee字段与多对一关联相关联 我的实际JSON如下所示。表1及其名称为“公司” 表2及其名称为“雇员” 我可以使用命令在公司表中获取直接字段作为别名 Company.aggregate([ {$match:{}}, {$project:{c_name:"$comp_name",id:1}} ]) 但我无法在公司表中

我正在使用mongoDB和newtonative命令,我喜欢使用别名作为字段名

这里我使用两个表(Company和employee),在Company表中,employee字段与多对一关联相关联

我的实际JSON如下所示。表1及其名称为“公司”

表2及其名称为“雇员”

我可以使用命令在公司表中获取直接字段作为别名

Company.aggregate([
    {$match:{}},
    {$project:{c_name:"$comp_name",id:1}}
])
但我无法在公司表中的雇主中获取状态为“活动”的数据

我期望的JSON是, 表1及其名称为“公司”

表2及其名称为“雇员”


这是你公司的收藏品

db.company.aggregate([
{ $match:{ status: "Active"} }, // to only get the documents that have ths status
{ $unwind: "$employers"}, // to flatten your array
{ $match:{ "employers.status": "Active"} }, // match again for active employers
{ $project: { // for the final structure
    c_name: "$comp_name",
    emp: ["$employers"],
    st: "$status",
    id: 1
  }
}
])

可能是的重复项,但它没有达到我的要求,并且它的输出为null,我的要求主要属于每个字段的别名,请帮助我,这是在
find()
query?@ZachSmith中进行投影的方法:
db.getCollection(“公司”).find({employers:{$elemMatch:{status:“Active”}},{comp_name:1,“employers.$”:1,status:1,id:1})
Company.aggregate([
    {$match:{}},
    {$project:{c_name:"$comp_name",id:1}}
])
c_name : "YYYY",
emp : [
                {
                    n : "Mike",
                    st : "Active",
                    id : 01
                }
            ],
st : "Active",
id : 00001          
{
    n : "Mike",
    st : "Active",
    id : 01,
    comp : {
                n : "YYYY",
                st : "Active",
                id : 00001
            }
}
db.company.aggregate([
{ $match:{ status: "Active"} }, // to only get the documents that have ths status
{ $unwind: "$employers"}, // to flatten your array
{ $match:{ "employers.status": "Active"} }, // match again for active employers
{ $project: { // for the final structure
    c_name: "$comp_name",
    emp: ["$employers"],
    st: "$status",
    id: 1
  }
}
])