亲爱,

我在使用mongoose回复mongoDB聚合时遇到了麻烦 .

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

UPDATE I found the fix, I have to add a 3rd parameter when setting the model

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

这是代码

Schema 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 - edited after first feedbacks

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