These are chat archives for Automattic/mongoose

20th
Feb 2018
Yogesh Bansal
@yogeshbansal
Feb 20 2018 02:53
Hello Everyone, I have 10 field in a document, Let suppose field 1 have 10 possible values from A-J. How to find the distinct value with there count like A: 5 B: 4 : C:10 D:1 E:0 .. so on
A:5 mean = 5 times 'A' present in whole collection
Mihajlo Ilijić
@Pritilender
Feb 20 2018 08:54
@yogeshbansal you can do it with aggregate. It's basic usage of $group operator. Let's assume that the name of your collection is Item, and the name of your 1st field is name, the query follows:
Item.aggregate([
  {
    $group: {
      _id: '$name',
      count: {
        $sum: 1,
      },
    },
])
You can read more here about $group operator: https://docs.mongodb.com/manual/reference/operator/aggregation/group/
Kev
@lineus
Feb 20 2018 09:05
@Pritilender that's an awesome answer! I was just figuring out how to answer this question and I ended up using mongodb.mongoclient with a call to distinct and a call to count for each distinct value gist
here's an implementation of your answer gist thank you @Pritilender !
Kev
@lineus
Feb 20 2018 09:21
I also just learned that naming a collection 'stats' in mongodb is a bad idea, lol.
Mihajlo Ilijić
@Pritilender
Feb 20 2018 09:38
@lineus you should note that you also have distinct on your mongoose model. No need for native driver.
Mihajlo Ilijić
@Pritilender
Feb 20 2018 09:45
Also note that if you don't want to get your documents as {_id: 'A', count: 1}, you can add $project stage after $group:
Item.aggregate([
      {
        $group: {
          _id: '$name',
          count: {
            $sum: 1,
          },
        },
      },
      {
        $project: {
          _id: 0, // exclude _id
          name: '$_id',
          count: '$count',
        }
      }
    ])
If you would like to get a key-value pair (like { A: 1, B: 5...}), you can write simple reduce:
result = Item.aggregate... // your query
result.reduce((acc, curr) => ({...acc, [curr.name]: curr.count]}), {})
Mihajlo Ilijić
@Pritilender
Feb 20 2018 09:50
But if you're reducing array of results to object, I would skip $project stage and use only $group. It's unneeded in my opinion. You know that the aggregation will return _id and count, so why rename _id to name.
Kev
@lineus
Feb 20 2018 09:53
I went for the native driver partly out of a desire to explore using it, as I haven't done much of that yet, but mostly because I figured that if @yogeshbansal didn't want to manipulate or consume the actual data within the documents, mongoclient would be a more lightweight choice.
Mihajlo Ilijić
@Pritilender
Feb 20 2018 09:54
But we're on mongoose gitter, so I guess that any question that pops up is regarding how to achieve something using mongoose :smile:
Kev
@lineus
Feb 20 2018 09:55
yeah, your answer was waaaaay better for many reasons! :)
Mihajlo Ilijić
@Pritilender
Feb 20 2018 09:55
Thanks :)
Kev
@lineus
Feb 20 2018 10:03
Thank you, I'm here to learn. You just showed me more in 5 minutes than I would have found in 5 hours!
Mihajlo Ilijić
@Pritilender
Feb 20 2018 15:11
Is there anyone using Mongoose with TypeScript? I need a bit of help how to make my custom plugin export proper types and how to make my model inherit them on plugin load. Right now, I have my own type defined in the plugin and just extend the base interface. Is there some better way to do it? Google only gives me that SO answer with mongoose-passport-local implementation, and I'm not sure I need my own .d.ts...
Vlado Tesanovic
@vladotesanovic
Feb 20 2018 19:23
@Pritilender Evening, i m using mongoose with typescript all time
It would be perfect if you can create small github repository with problem you have, than i can take a look... or anyone here
If you add custom plugin which have custom method, called whatever: myMethodThatDoSomething()
i cannot see how you can add that method to Model of Mongoose, you need to add it to your interface which extends Model
You probably do that already
Mihajlo Ilijić
@Pritilender
Feb 20 2018 23:12
@vladotesanovic Yes, I that already. I thought there's some shortcut...