1

I have a collection having following documents -

    {"_id":ObjectId("xxx"),"article_Id":"1","publish_Time":NumberLong(10)}  
    {"_id":ObjectId("yyy"),"article_Id":"1","publish_Time":NumberLong(11)}  
    {"_id":ObjectId("zzz"),"article_Id":"1","publish_Time":NumberLong(12)}  

I want to aggregate on article_id and desired result is somewhat like -

  {"_id":{"articleId":"1"}, pub_t:[publish_Time":NumberLong(10),publish_Time":NumberLong(11),publish_Time":NumberLong(10)]}

I want to combine the publish_time field in separate documents to one array in resultant. How can I do this? Thanks in Advance.

2
  • Were you able to use the query I provided in my answer? Commented Dec 16, 2015 at 10:38
  • I used $addToSet. It worked fine for me. Commented Jan 19, 2016 at 5:29

1 Answer 1

1

Query

db.articles.aggregate(
{
    $group:
    {
        _id:
        {
            articleId: "$article_Id"
        },
        pub_t:
        {
            $push: "$publish_Time"
        }
    }
})

Result

{
    "_id" : {
        "articleId" : "1"
    },
    "pub_t" : [
        NumberLong(10),
        NumberLong(11),
        NumberLong(12)
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

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.