mongoose聚合不支持writeConcern

我正在尝试计算给定用户未读的通知数量。

这就是说,vía aggregate ,我已经做了下一个代码在控制台输出预期的结果:

 db.getCollection('users').aggregate([ { $match: { _id: ObjectId("5847f61a825d024ac9e3d08c")}}, { $unwind: '$notifications'}, { $match: {'notifications.read': {$eq: 0}}}, { $group: { _id: '$_id', notifications: {$push: '$notifications._id'}}}]) 

其输出:

 { "_id" : ObjectId("5847f61a825d024ac9e3d08c"), "notifications" : [ ObjectId("5847fefb708b3b4c2cf9e8fe"), ObjectId("5847fefe708b3b4c2cf9e900") ] } 

所以我试图做同样的nodeJSmongoose ,所以我有一个模型和一个static方法是完全一样的,我在我的最后一个查询:

 var UserSchema = new mongoose.Schema({ nickname: { type: String, trim: true}, username: { type: String, trim: true }, notifications: [{ a: { _id: { type: mongoose.Schema.Types.ObjectId, ref: 'x' }, x: { type: mongoose.Schema.Types.ObjectId, ref: 'y' } }, b: { _id: { type: mongoose.Schema.Types.ObjectId, ref: 'y' }, x: { type: mongoose.Schema.Types.ObjectId, ref: 'y' } }, read: { type: Number, default: 0 }, // 0 - Unread, 1 - read ts: { type: Date, default: Date.now } }] }, { timestamps: { createdAt: 'created_at' } }); UserSchema.statics.getCountNotifications = function (user_id) { return new Promise(function (resolve, reject) { mongoose.model('User', UserSchema).aggregate([ { $match: { _id: mongoose.Types.ObjectId(user_id)}}, { $unwind: '$notifications'}, { $match: {'notifications.read': {$eq: 0}}}, { $group: { _id: '$_id', notifications: {$push: '$notifications._id'}}}], function (err, data) { if (err) { reject(err); } else { resolve(data); } }); }); }; 

不幸的是,这给我输出了下一个错误: MongoError: Command does not support writeConcern 。 事实上,检查日志这是输出:

 Unhandled rejection MongoError: Command does not support writeConcern at Function.MongoError.create (/Users/Project/api/node_modules/mongodb-core/lib/error.js:31:11) at /Users/Project/api/node_modules/mongodb-core/lib/connection/pool.js:462:72 at authenticateStragglers (/Users/Project/api/node_modules/mongodb-core/lib/connection/pool.js:410:16) at .messageHandler (/Users/Project/api/node_modules/mongodb-core/lib/connection/pool.js:444:5) at Socket.<anonymous> (/Users/Project/api/node_modules/mongodb-core/lib/connection/connection.js:306:22) at emitOne (events.js:96:13) at Socket.emit (events.js:188:7) at readableAddChunk (_stream_readable.js:172:18) at Socket.Readable.push (_stream_readable.js:130:10) at TCP.onread (net.js:542:20) From previous event: at Function.UserSchema.statics.getCountNotifications (/Users/Project/api/models/user.js:301:10) at /Users/Project/api/routes.js:563:8 at Layer.handle [as handle_request] (/Users/Project/api/node_modules/express/lib/router/layer.js:95:5) at next (/Users/Project/api/node_modules/express/lib/router/route.js:131:13) at /Users/Project/api/models/user.js:179:7 at JwtStrategy.strategy.success (/Users/Project/api/node_modules/passport/lib/middleware/authenticate.js:201:18) at verified (/Users/Project/api/node_modules/passport-jwt/lib/strategy.js:102:33) at /Users/Project/api/app.js:62:7 at Query.<anonymous> (/Users/Project/api/node_modules/mongoose/lib/model.js:3336:16) at /Users/Project/api/node_modules/kareem/index.js:259:21 at /Users/Project/api/node_modules/kareem/index.js:127:16 at _combinedTickCallback (internal/process/next_tick.js:67:7) at process._tickDomainCallback (internal/process/next_tick.js:122:9) 

任何帮助?

感谢您的build议。

深入探讨问题后,问题是我的mongoose的版本。 更新到4.7.1并且跟随mongoose文档,它运作象魅力。 在这里,我留下了我的代码的最终版本:

 this.aggregate({ $match: { _id: mongoose.Types.ObjectId(user_id)}}) .unwind('notifications') .match({ 'notifications.read': { $eq: 0 } }) .group({ _id: '$_id', notifications: { $push: '$notifications._id'} }) .exec();