如何从 mongodb 中找到 select 个不同的值

How to find select distinct values from mongodb

如何在 angularJS 中使用 find select distinct 然后计算它,我是 mean-stack 的新手,我不知道如何使用 distinct 方法。

这是我的 table 结构和示例数据:

donation_no:"3124"
donors_name:"Jeremy Adrian De Vera"
date:"12/26/2018"
time:"8:30"
blood_group:"B"
volume:"2"
branch:"Pasig"

我已经像这样使用 php 尝试过,但是我怎样才能在 angular 中做到这一点:

$query = "SELECT DISTINCT fruits, COUNT(*) as counter FROM my_table WHERE date_tested BETWEEN '$date_from' AND '$date_to' GROUP BY fruits";

这是我从数据库中查找值的代码

api.js

router.get('/blooddonationmanagement', function(req, res) {

    Blooddonation.find({},function(err, blooddonations) {
        res.json({ success: true, blooddonations: blooddonations });
    });   
});

blooddonationCtrl.js

angular.module('blooddonationControllers', [])
.controller('blooddonationCtrl', function(Blooddonation,$scope) {
    var app = this;
    function getBlooddonations() {

        Blooddonation.getUsers().then(function(data) {    
            app.blooddonations = data.data.blooddonations;  
    });
}

chapters.html

<tr ng-repeat="person in blooddonationmanagement.blooddonations">
    <td>{{ person.branch}}</td>   
</tr>

我如何计算不同的值然后从我的数据库中计算它,我期待这样:

branch       count
Pasig         29
Manila        16
Pasay         19

你应该试试distinct()

Blooddonation.distinct('branch',... );

只需阅读此处的文档:https://docs.mongodb.com/manual/reference/method/db.collection.distinct/

您可以使用:

db.collection.distinct()