【问题标题】:How to find select distinct values from mongodb如何从 mongodb 中找到选择不同的值
【发布时间】:2019-05-21 16:23:04
【问题描述】:

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

这是我的带有示例数据的表结构:

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 });
    });   
});

献血Ctrl.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

【问题讨论】:

  • 你能发布你的表结构以及示例数据吗?
  • 好的,我会更新我的问题
  • 已更新,我正在为分支找到不同的值

标签: angularjs node.js mongodb mean-stack


【解决方案1】:

你应该试试distinct()

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

【讨论】:

    【解决方案2】:

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

    您可以使用:

    db.collection.distinct()
    

    【讨论】:

    • 我会试试这个。
    猜你喜欢
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    • 1970-01-01
    • 2021-12-29
    • 2017-08-21
    • 2014-12-11
    相关资源
    最近更新 更多