2016-12-07 15 views
1

特定のユーザーに対して、未読の通知の数をカウントしようとしています。mongoose集計がサポートされていませんwriteConcern

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") 
    ] 
} 

だから、私がやろうとしていると述べた

を介して aggregateは、私は出力期待される結果は、そのコンソールに次のコードをやりました 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) 

これに関する助けがありますか?

アドバイスありがとうございます。

答えて

2

問題を掘り下げた後、問題は私の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(); 
+0

ありがとうございます。おそらく、これに1時間掘り起こしてくれました。 –

関連する問題