0

I have a document like this: this is my example data is attached below,

[
    {
        "_id": ObjectId("6218b836405919280c209f7e"),
        "projectId": ObjectId("6218a31f405919280c209e18"),
        "accountId": ObjectId("621888e852bd8836c04b8f82"),
        "personalIdRoot": [
            {
                "_id": ObjectId("6221e7514195b43f24c9953f"),
                "personalId": ObjectId("6218b48c405919280c209f6c"),
            },
            {
                "_id": ObjectId("6221e7514195b43f24c99540"),
                "personalId": ObjectId("621ef1e40bd3a220f487cd96"),
            }
        ],
        "personalIdFill": [
            {
                "_id": ObjectId("6221e7514195b43f24c9953d"),
                "personalId": ObjectId("6218b48c405919280c209f6c"),
            },
            {
                "_id": ObjectId("6221e7514195b43f24c9953e"),
                "personalId": ObjectId("621ef1e40bd3a220f487cd96"),
            }
        ],
        "personalIdCap": [
            {
                "_id": ObjectId("6221e7514195b43f24c9953b"),
                "personalId": ObjectId("6218b48c405919280c209f6c"),
            },
            {
                "_id": ObjectId("6221e7514195b43f24c9953c"),
                "personalId": ObjectId("621ef1e40bd3a220f487cd96"),
            }
        ],
        "aps": ObjectId("6218bc18405919280c209f8e"),
    }
]

i used this example data for my aggregate query. my aggregate query is attached below: please find below code:

db.getCollection('funds').aggregate([
    {
        $match: {
            accountId: ObjectId("621888e852bd8836c04b8f82"),
            projectId: ObjectId("6218a31f405919280c209e18"),
            aps: {
                $in: [
                    ObjectId("6218bc18405919280c209f8e")
                ]
            }

        }
    },
    {
        $facet: {
            "results": [
                {
                    $group: {
                        _id: 0,
                        values: { "$addToSet": "$personalIdRoot.welderId" },
                        values: { "$addToSet": "$personalIdFill.welderId" },
                        values: { "$addToSet": "$personalIdCap.welderId" }
                    }
                },
            ],
        }
    }
])

my result: 

/* 1 */
{
    "results" : [ 
        {
            "_id" : 0.0,
            "values" : [ 
                [ 
                    ObjectId("6218b48c405919280c209f6c")
                ], 
                [ 
                    ObjectId("6218b2e4405919280c209f68")
                ], 
                [ 
                    ObjectId("6218b48c405919280c209f6c"), 
                    ObjectId("621ef1e40bd3a220f487cd96")
                ]
            ]
        }
    ]
}

But i need my result in a single query:


/* 1 */
{
    "results" : [ 
        {
            "_id" : 0.0,
            "values" : [ 
                    ObjectId("6218b48c405919280c209f6c"),
                    ObjectId("621ef1e40bd3a220f487cd96")
            ]
        }
    ]
}

I have a result of array of array collections. but i need it in a single array. like above result

Thanks in advance.

1 Answer 1

1

It sounds like what you want is:

db.collection.aggregate([
  {
    $match: {
      accountId: ObjectId("621888e852bd8836c04b8f82"),
      projectId: ObjectId("6218a31f405919280c209e18"),
      aps: {
        $in: [
          ObjectId("6218bc18405919280c209f8e")
        ]
      }
    }
  },
  {
    $project: {
      values: {
        $setUnion: [
          "$personalIdCap.personalId",
          "$personalIdFill.personalId",
          "$personalIdRoot.personalId"
        ]
      }
    }
  }
])

See how it works on the playground example

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply. But i need every id in a single array with unioque values. But your code shows in seperate ids
Your question is unclear. Your data does not match your expected result. Please update your question. There is no ObjectId("6218b2e4405919280c209f68") in your sample data
okay i edit the result . I dont need this ObjectId

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.