简体   繁体   中英

How to find select distinct values from mongodb

How can I use find select distinct in angularJS then counting it, I'm new to mean-stack and I don't know how to use distinct method.

Here is my table structure with sample data:

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

I already tried it using php like this but how can I do this in angular:

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

Here is my code for finding the values from my db

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>

How can I count the distinct values then counting it from my db, Im expecting like this:

branch       count
Pasig         29
Manila        16
Pasay         19

你应该尝试distinct()

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

Just read the docs here: https://docs.mongodb.com/manual/reference/method/db.collection.distinct/

You can use:

db.collection.distinct()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM