0

I am using mongoDB and new to native command, I like to use alias name for the field name

here I am using two tables (Company and employee), In company table employee fields are associated with many to one association

My actual JSON is as follows. Table one and its name is "company"

comp_name : "YYYY",
employers : [
                {
                    name : "Mike",
                    status : "Active",
                    id : 01
                },{
                    name : "San",
                    status: "InActive",
                    id : 02
                }
            ],
status : "Active",
id : 00001          

Table 2 and its name is "employee"

{
    name : "Mike",
    status : "Active",
    id : 01,
    company : {
                name : "YYYY",
                status : "Active",
                id : 00001
            }
},
{
    name : "San",
    status: "InActive",
    id : 02,
    company : {
                name : "YYYY",
                status : "Active",
                id : 00001
            }
}

I able to get the direct field as alias name in company table using the command

Company.aggregate([
    {$match:{}},
    {$project:{c_name:"$comp_name",id:1}}
])

but i can't acheive it the data in employers in company table which status is "Active"

My expected JSON is, Table one and its name is "company"

c_name : "YYYY",
emp : [
                {
                    n : "Mike",
                    st : "Active",
                    id : 01
                }
            ],
st : "Active",
id : 00001          

Table 2 and its name is "employee"

{
    n : "Mike",
    st : "Active",
    id : 01,
    comp : {
                n : "YYYY",
                st : "Active",
                id : 00001
            }
}
halfer
  • 18,701
  • 13
  • 79
  • 158
Aravindh
  • 281
  • 1
  • 3
  • 14
  • Possible duplicate of [Retrieve only the queried element in an object array in MongoDB collection](https://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection) – s7vr Dec 14 '17 at 13:17
  • but it doesn't reach my requirement, and also it gives null as output, my requirement is mainly belongs to alias name for every field, please help me out this – Aravindh Dec 14 '17 at 13:52

1 Answers1

1

This would be for your company collection

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
  }
}
])
Alex P.
  • 2,651
  • 3
  • 15
  • 25
  • Is there a way to project in a `find()` query? – Zach Smith Nov 19 '18 at 12:00
  • 1
    @ZachSmith I think this is how you could do it with projection: `db.getCollection("company").find({employers: {$elemMatch: {status: "Active"}}}, {comp_name: 1, "employers.$": 1, status: 1, id: 1})` – Alex P. Nov 19 '18 at 14:38