How to sum distinct values of a field in a MongoDB collection (utilizing mongoose)

aaki

Imagine I had a collection called journals containing documents like the following:

{
  "article": "id1",
  "d": 2
},
{
  "article": "id1",
  "d": 2
},
{
  "article": "id1",
  "d": 3
},
{
  "article": "id2",
  "d": 2
},
...

Where d is kind of a switch and article is a reference. Now I want to have a result like the following:

[
  {
    "_id": "id1",
    "total": 3,
    "d2": 2,
    "d3": 1
  },
  {
    "_id": "id2",
    "total": 1,
    "d2": 1,
    "d3": 0
  }
]

I'm using mongoose and have a model called Journal. What I've got so far is…

Journal.aggregate(
  { $project: {
    articleId: 1,
    depth2 : { $gte:['$d', 2] },
    depth3 : { $eq:['$d', 3] }
  }},
  { $group: {
      _id: '$article',
      total: { $sum: 1 },
      d2: { $sum: '$depth2'},
      d3: { $sum: '$depth3'}
    }
  },
  function (err, journal) {
    console.log(journal);
  }
);

which results in:

[
  {
    "_id": "id1",
    "total": 3,
    "d2": 0,
    "d3": 0
  },
  {
    "_id": "id2",
    "total": 1,
    "d2": 0,
    "d3": 0
  }
]

Obviously the error here is that $eq:['$d', 3] is not summed up because that results in a boolean.

So is there a better expression that projects the new depth2 and depth3 fields to 1or 0 instead of true or false?
Or is there a complete and better approach? :)

I'd like to avoid making 3 queries and prepending a matching phase like { $match: { d: 2 } }.

JohnnyHK

You can use $cond to convert a boolean to a numerical value, but you've also got a $gte where it seems an $eq should be and your docs use article while your code uses articleId:

Journal.aggregate([
  { $project: {
    article: 1,
    depth2 : { $cond: [{$eq: ['$d', 2]}, 1, 0] },
    depth3 : { $cond: [{$eq: ['$d', 3]}, 1, 0] }
  }},
  { $group: {
      _id: '$article',
      total: { $sum: 1 },
      d2: { $sum: '$depth2'},
      d3: { $sum: '$depth3'}
    }
  }
]);

Output:

{
    "result" : [ 
        {
            "_id" : "id2",
            "total" : 1,
            "d2" : 1,
            "d3" : 0
        }, 
        {
            "_id" : "id1",
            "total" : 3,
            "d2" : 2,
            "d3" : 1
        }
    ],
    "ok" : 1
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to sum distinct values of a field in a MongoDB collection (utilizing mongoose)

From Dev

Backbone collection: Retrieve distinct values of a collection and sum the distinct values

From Dev

Backbone collection: Retrieve distinct values of a collection and sum the distinct values

From Dev

Meteor: How to show distinct field values from a collection in a select dropdown?

From Dev

how to count number of distinct values of a field from two collections in mongodb

From Dev

Meteor: how to search for only distinct field values aka a collection.distinct("fieldname") similar to Mongo's

From Dev

Distinct values from various fields in MongoDB collection

From Dev

How to find documents in mongodb collection which has a field with 2 values?

From Dev

Find all the non distinct values of a field in mongodb

From Dev

How do you get distinct values from dataTables and sum the total specific field using JS

From Java

MongoDB - select distinct values from a collection where values are separated by comma

From Dev

Query to group distinct values and show sum of array values in mongodb

From Dev

Aggregation Sum Distinct documents from array field MongoDB

From Dev

How to count distinct values from a collection in Laravel?

From Dev

How to count distinct values of a reference collection in mongo

From Dev

How do I sum distinct values?

From Dev

How to select sum of count of distinct values in mysql?

From Dev

How to count the sum of distinct Excel values in a column?

From Dev

How to populate in 3 collection in mongoDB with Mongoose

From Dev

distinct sum does not distinct values

From Dev

Mongodb sum with distinct

From Dev

MongoDB - How to define multiple datatypes for a field in Mongoose?

From Dev

how to find distinct field of a model upon an and condition in mongoose?

From Dev

How to count distinct date items in a timestamp field in Mongoose/NodeJS?

From Dev

how to find distinct field of a model upon an and condition in mongoose?

From Dev

how to get distinct values in mongodb using golang

From Dev

MongoDB: Counting how many of each distinct values there are?

From Dev

Adding an array field to a collection based on values of another array field in mongodb

From Dev

Sum for Distinct values in MySQL

Related Related

  1. 1

    How to sum distinct values of a field in a MongoDB collection (utilizing mongoose)

  2. 2

    Backbone collection: Retrieve distinct values of a collection and sum the distinct values

  3. 3

    Backbone collection: Retrieve distinct values of a collection and sum the distinct values

  4. 4

    Meteor: How to show distinct field values from a collection in a select dropdown?

  5. 5

    how to count number of distinct values of a field from two collections in mongodb

  6. 6

    Meteor: how to search for only distinct field values aka a collection.distinct("fieldname") similar to Mongo's

  7. 7

    Distinct values from various fields in MongoDB collection

  8. 8

    How to find documents in mongodb collection which has a field with 2 values?

  9. 9

    Find all the non distinct values of a field in mongodb

  10. 10

    How do you get distinct values from dataTables and sum the total specific field using JS

  11. 11

    MongoDB - select distinct values from a collection where values are separated by comma

  12. 12

    Query to group distinct values and show sum of array values in mongodb

  13. 13

    Aggregation Sum Distinct documents from array field MongoDB

  14. 14

    How to count distinct values from a collection in Laravel?

  15. 15

    How to count distinct values of a reference collection in mongo

  16. 16

    How do I sum distinct values?

  17. 17

    How to select sum of count of distinct values in mysql?

  18. 18

    How to count the sum of distinct Excel values in a column?

  19. 19

    How to populate in 3 collection in mongoDB with Mongoose

  20. 20

    distinct sum does not distinct values

  21. 21

    Mongodb sum with distinct

  22. 22

    MongoDB - How to define multiple datatypes for a field in Mongoose?

  23. 23

    how to find distinct field of a model upon an and condition in mongoose?

  24. 24

    How to count distinct date items in a timestamp field in Mongoose/NodeJS?

  25. 25

    how to find distinct field of a model upon an and condition in mongoose?

  26. 26

    how to get distinct values in mongodb using golang

  27. 27

    MongoDB: Counting how many of each distinct values there are?

  28. 28

    Adding an array field to a collection based on values of another array field in mongodb

  29. 29

    Sum for Distinct values in MySQL

HotTag

Archive