Generating ObjectID's in Aggregate

Viewed 5433

I have a collection called people (see below) that contains their personal details and skills. From that collection I want to create a new collection (which regularly updates) with the total amount of occurences for a certain skill filled with an ObjectID. The skills in the people collection should be updated with the right Object ID.

Right now I have the following query:

db.people.aggregate([ 
    { $unwind: "$expertise" },
    { $group : { 
        _id: "$expertise.item",
        count: { $sum: 1 },
        people: { $push: "$_id" } 
        } 
    }
])

This gives me the new collection, but unfortunately I cannot generate a unique objectID in that query. It keeps telling me that an accumulator object is expected.

How do I generate the skills database with unique ObjectID's per skill and update the people collection with the right skill ObjectID's?


People

A collection with people, their personal details and their specific skillsets. I've removed most of the data for this example.

{
"_id" : ObjectId("591c5d9be75d8a0d34f61786"),
"id" : 1,
"username" : "user@name.com",
"expertise" : [ 
    {
        "niveau" : 5,
        "item" : "High Availability Systems"
    }, 
    {
        "niveau" : 8,
        "item" : "Object Oriented Design"
    }
],
"function" : "functionname"
},
{
"_id" : ObjectId("591c5d9be75d8a0d34f61787"),
"id" : 2,
"username" : "user@name.com",
"expertise" : [ 
    {
        "niveau" : 9,
        "item" : "High Availability Systems"
    }, 
    {
        "niveau" : 4,
        "item" : "Java"
    }
],
"function" : "functionname"
}

New Collection: Skills

From this collection I would like to create a new collection, where the objectID's of people are reused in an array. It should result in something like this:

{
    "_id" : ObjectId("591c5d9be75d8a0d456324"),
    "item" : "High Availability Systems",
    "people" : [ 
        "591c5d9be75d8a0d34f61786",
        "591c5d9be75d8a0d34f61787"
    ],
    "count" : 2
},
{
    "_id" : ObjectId("591c5d9be75d8a0d451234"),
    "item" : "Java",
    "people" : [ 
        "591c5d9be75d8a0d34f61786"
    ],
    "count" : 1
}
1 Answers

Literally copying from the comments so its visible as an answer

Assigning zero to the _id field causes mongodb to generate an ObjectId.

db.people.aggregate([
   {
      "$unwind":"$expertise"
   },
   {
      "$group":{
         "_id":"$expertise.item",
         "count":{
            "$sum":1
         },
         "people":{
            "$push":"$_id"
         }
      }
   },
   {
      "$project":{
         "_id":0,
         "item":"$_id",
         "count":1,
         "people":1
      }
   },
   {
      "$out":"newskills"
   }
])
Related