猫鼬:获取聚合返回空数组

时间:2018-12-09 15:14:19

标签: express mongoose aggregate

亲爱的

使用猫鼬无法恢复mongoDB聚合。

MongoDB中的查询正在发送回数据,但是来自mongoose的调用总是返回空数组

更新 我找到了解决方法,设置模型时必须添加第三个参数

const RawData = mongoose.model('rawdata', rawDataSchema, 'rawdata');

这是代码

架构rawdata.js

import mongoose from 'mongoose';

const rawDataSchema = new mongoose.Schema({

    Email: String,
    Nb: Number,
    Licences: String,
    Licence_ret: String,
    Pays: String,
    Share: String,
    Pack: String,
    Quota_in_MB: Number,
    Webmail_Ext: Number, });


const RawData = mongoose.model('rawdata', rawDataSchema);

export default RawData;

index.js-在首次反馈后进行编辑

import express from 'express';
import mongoose from 'mongoose';
import RawData from 'rawdata'

const app = express();

app.get('/test', async (req, res) => {
    const data = await RawData.aggregate(
        [
            { "$group": { _id: { license: "$Licence_Carrefour" }, count: { $sum: 1 } } },
            {
                $project: {
                    _id: 0,
                    License: "$_id.license",
                    count: 1
                }
            }
        ]);
    console.log('data', data)
    res.send('hello')
});

(async () => {
    await mongoose.connect(dbURI, { useNewUrlParser: true }, err => {
        if (err) console.log('\x1b[31m%s', 'DB is not connected');
        console.log('connected to database ');
    });

    app.listen(APP_PORT, () => console.log('app up'));
})();

0 个答案:

没有答案