gpt4 book ai didi

node.js - Mongoose 聚合不支持 writeConcern

转载 作者:可可西里 更新时间:2023-11-01 10:02:43 26 4
gpt4 key购买 nike

我正在尝试计算给定用户有多少条未读通知。

也就是说,通过 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)

有什么帮助吗?

谢谢你的建议。

最佳答案

深入研究后,问题出在我的 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();

关于node.js - Mongoose 聚合不支持 writeConcern,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41026170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com