我对MongoDB,Mongoose和Node.js有疑问。
我使用猫鼬有这种模式:
const EmoteSchema = new mongoose.Schema({
guild_id: String,
users: [{
user_id: String,
emotes: [{
emote: String,
channels: [String],
cooldown: Number,
all_channels: { type: Boolean, default: true },
global_cooldown: { type: Boolean, default: true },
lock: { type: Boolean, default: false }
}]
}]
});
它看起来像MongoDB:
{
"_id" : ObjectId("5ee6776b9277898982b0a25a"),
"guild_id" : "714931095929618443",
"users" : [
{
"_id" : ObjectId("5ee6776b9277898982b0a25b"),
"user_id" : "160230784257622016",
"emotes" : [
{
"channels" : [ ],
"all_channels" : true,
"global_cooldown" : true,
"lock" : false,
"_id" : ObjectId("5ee6776b9277898982b0a25c"),
"emote" : "?"
}
]
}
],
"__v" : 0
}
我要实现的目的是基于三个条件来更新数据库: 发送guild_id,user_id和emote的信息时:
这是我试图做的事情
let filter = {
guild_id: guild_inp,
"users.user_id": user_inp
};
let update = {
$push: { "users.$[i].emotes": { emote: emote_inp } }
};
let options = {
arrayFilters: [
{ "i.users.user_id": user_inp }
],
rawResult: true
};
Emote.findOneAndUpdate(filter, update, options, fn);
如果行会代码:714931095929618443,用户输入代码:160230784257622016,并表达: 回调函数得到以下结果:
RES: {
lastErrorObject: { n: 1, updatedExisting: true },
value: {
_id: 5ee6776b9277898982b0a25a,
guild_id: '714931095929618443',
users: [ [Object] ],
__v: 0
},
ok: 1
}
它说它已经更新了?但是我看不到MongoDB发生任何变化!
如果有一种更轻松的方法来实现我正在做的事情(例如新模式),请提供帮助!