Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB aggregation: Group all records into a single result

In a mongodb aggregation call, how do I use the $group operator to group all documents in the pipeline into a single result?

Let's say I have a collection of records that look like this:

{
    user_id: 234,
    signup_date: 2013-06-27
}

I want to use the aggregate function to query the database for a list of users who signed up in a given date range, and return it as a list. I'd like a result that looks like this:

{
    users: [234, 235, 236]
}

My query looks something like this:

db.users.aggregate([

    { $match: {
        signup_date: {
            $gte: date_begin_variable,
            $lt: date_end_variable
    }},

So far, so good. I now have a subset of records, all of which have a signup date in the desired range. But here's where I get into trouble. Now I want to group ALL those records into a single result containing a list of all the IDs. But I don't know how to do that using $group.

    { $group: {
        _id: null
        users: { $addToSet: user_id }
    }}

])

When I tried _id: null, I got back an empty list of user IDs.

I could just return the desired set of records using find(), and then just process the IDs into a list manually, but doing it using aggregate() seems cleaner. Am I wrong about that? What would be the best way to accomplish what I'm describing, either using aggregate or not?

like image 852
ekl Avatar asked Jun 27 '13 22:06

ekl


People also ask

What does MongoDB aggregation return?

Returns: A cursor to the documents produced by the final stage of the aggregation pipeline operation, or if you include the explain option, the document that provides details on the processing of the aggregation operation. If the pipeline includes the $out operator, aggregate() returns an empty cursor.

Which aggregation method is preferred for use by MongoDB?

The pipeline provides efficient data aggregation using native operations within MongoDB, and is the preferred method for data aggregation in MongoDB. The aggregation pipeline can operate on a sharded collection.

Is MongoDB good at aggregation?

As with many other database systems, MongoDB allows you to perform a variety of aggregation operations. These allow you to process data records in a variety of ways, such as grouping data, sorting data into a specific order, or restructuring returned documents, as well as filtering data as one might with a query.

What is unwind in MongoDB aggregation?

The operation unwinds the sizes array and includes the array index in the new arrayIndex field. If the sizes field does not resolve to a populated array but is not missing, null, or an empty array, the arrayIndex field is null .


1 Answers

Try this query:

db.users.aggregate([
  {
    $match: {
      signup_date: {
        $gte: date_begin_variable,
        $lt: date_end_variable
      }
    }
  },
  {
    $group: {
      _id: null,
      users: {
        $addToSet: "$user_id"
      }
    }
  }
])
like image 119
Rinat Suleimanov Avatar answered Oct 29 '22 06:10

Rinat Suleimanov