giorgio-zamparelli / mongo-in-memory

in memory mocking engine for mongo db
25 stars 4 forks source link

Add support for aggregation #2

Open rsadyhov opened 7 years ago

rsadyhov commented 7 years ago

Getting next error for aggregation operation: "MongoError: The 'cursor' option is required, except for aggregation explain\n at Function.MongoError.create (api/node_modules/mongodb-core/lib/error.js:31:11)\n at /api/node_modules/mongodb-core/lib/connection/pool.js:489:72\n at authenticateStragglers (api/node_modules/mongodb-core/lib/connection/pool.js:435:16)\n at Connection.messageHandler (api/node_modules/mongodb-core/lib/connection/pool.js:469:5)\n at Socket.<anonymous> (/api/node_modules/mongodb-core/lib/connection/connection.js:321:22)\n at emitOne (events.js:96:13)\n at Socket.emit (events.js:188:7)\n at readableAddChunk (_stream_readable.js:176:18)\n at Socket.Readable.push (_stream_readable.js:134:10)\n at TCP.onread (net.js:548:20)"

List of dependencies:

"dependencies": {
    "bluebird": "^3.4.6",
    "body-parser": "^1.15.2",
    "express": "^4.14.0",
    "fs-promise": "^0.5.0",
    "log4js": "^1.1.1",
    "mongoose": "^4.6.0",
    "nconf": "^0.8.4",
    "newrelic": "^1.38.1",
    "pm2": "^1.1.3"
  },
  "devDependencies": {
    "chai": "^3.5.0",
    "chai-string": "^1.2.0",
    "fast-csv": "^2.1.0",
    "mocha": "^3.0.1",
    "mochawesome": "^1.4.0",
    "mongo-in-memory": "0.0.5",
    "nodemon": "^1.10.2",
    "request": "^2.74.0",
    "request-promise": "^4.1.1"
  }
giorgio-zamparelli commented 7 years ago

Hi @rsadyhov ! Could you post a executable snippet of your code using mongo-in-memory and causing that error so I can reproduce it and find out the problem?

rsadyhov commented 7 years ago

Hello,

I suppose it must be some versioning because this code doesn't work:

return dbKeyModel[dbKey].aggregate(query);

and this works fine for me:

return new Promise((resolve, reject) => {
            dbKeyModel[dbKey].aggregate(query).cursor({
              batchSize: 2500,
              async: true
            }).exec().then((cursor) => {
              let result = {};
              cursor
                .on('error', function(err) {
                  reject(err);
                })
                .on('data', function(data) {
                  result = Object.assign({}, result, data);
                })
                .on('end', function() {
                  resolve(Object.keys(result).length === 0 ? result : [result]);
                });
            });

Where dbKeyModel[dbKey] is next:

const mongoose = require('mongoose');

let someSchema = new mongoose.Schema({
    _id: String,
    sometId: {type: String, required: true},
    ........
    someNumber: Number
});

let SomeModel = mongoose.model('SomeModel', someSchema);
module.exports = SomeModel;