[英]Mongoose won't update sub-document
I've been banging my head against this for a few hours now and just can't seem to figure it out. 我已经为此奋斗了几个小时,但似乎无法弄清楚。 I decided to use Mongo on a learning project to see how I like it.
我决定在一个学习项目上使用Mongo,看看自己的感觉如何。 Mongoose came as the recommended way to use it, so I dove into that too.
猫鼬是推荐的使用方式,所以我也很喜欢。 I have two schemas:
我有两种模式:
var PluginSchema = new Schema({
name: { type: String, required: true },
slug: { type: String },
description: { type: String },
created_date: { type: Date, default: Date.now },
active: { type: Boolean, default: true },
user: { type: Schema.Types.ObjectId, ref: 'User' },
versions: [PluginVersion.schema]
});
PluginSchema.methods.getLatestVersion = function(callback) {
if(this.versions.length > 0) {
this.versions.sort(function(a, b) {
if(semver.gt(a.version, b.version)) {
return 1;
} else if(semver.lt(a.version, b.version)) {
return -1;
} else {
return 0;
}
});
callback(this.versions[this.versions.length-1]);
} else {
callback(undefined);
}
};
and 和
var PluginVersionSchema = new Schema({
version: { type: String, required: true },
downloads: { type: Number, default: 0 },
size: { type: Number, required: true },
updatedChecks: { type: Number, default: 0 },
fileName: { type: String, required: true },
uploadedDate: { type: Date, default: Date.now }
});
The issue here is the 'versions' relationship. 这里的问题是“版本”关系。 At some point I want to update a version of the Plugin.
在某些时候,我想更新插件的版本。 The thing I want to update is the
updatedChecks
field. 我要更新的是
updatedChecks
字段。 Basically just updatedChecks += 1
. 基本上只是
updatedChecks += 1
。
Plugin.findById(req.params.plugin_id)
.populate('user')
.populate('versions')
.exec(function(err, plugin) {
if(err) {
res.status(404);
res.send("Plugin not found.");
} else {
plugin.getLatestVersion(function(version) {
if(version !== undefined) {
pluginData = {
stuff: "that",
gets: "returned",
tothe: "user"
};
// This says 'Affected: 1'. As expected
PluginVersion.update({_id: version._id}, {
updatedChecks: version.updatedChecks + 1
}, function(err, affected) {
console.log("Affected: " + affected);
if(err) { res.send(err); }
res.status(200);
res.json(pluginData);
});
} else {
res.status(404);
res.send("No versions found for this plugin.");
}
});
}
});
So far, so good. 到现在为止还挺好。 However, when I try to access that version again via the Plugin schema, the
updatedChecks
value hasn't changed! 但是,当我尝试通过插件模式再次访问该版本时,
updatedChecks
值没有改变! I checked the _id
value on the version I'm updating versus the version that gets pulled from the Plugin.versions
field and they are they same. 我检查了我正在更新的版本与从
Plugin.versions
字段中提取的版本中的_id
值,它们是相同的。 Do I need to remove the version from Plugin.versions
and re-insert a new one with the updated value? 我需要从
Plugin.versions
删除该版本, Plugin.versions
使用更新后的值重新插入一个新版本吗? I also tried just updating the value and calling save()
but that didn't seem to work either. 我也尝试仅更新值并调用
save()
但这似乎也不起作用。
I ended up getting this work by accessing the plugin version directly from the Plugin object. 我最终通过直接从Plugin对象访问插件版本来完成这项工作。
var pluginIndex = false;
for(var i = 0; i < plugin.versions.length; i++) {
if(plugin.versions[i]._id === version._id) {
pluginIndex = i;
}
}
if(pluginIndex !== false) {
plugin.versions[pluginIndex].updatedChecks++;
plugin.versions[pluginIndex].markModified('updatedChecks');
plugin.versions[pluginIndex].save(function() {
plugin.markModified('versions');
plugin.save(function() {
res.status(200);
res.json(pluginData);
});
});
}
I also has a little help from: Mongoose save() not updating value in an array in database document 我还从以下方面获得了一些帮助: Mongoose save()不更新数据库文档中数组的值
The problem here is, your updating query is written wrong. 这里的问题是,您的更新查询写错了。 It must be
一定是
PluginVersion.update({
_id: version._id
}, {
$set: {
updatedChecks: version.updatedChecks + 1
}
}, function(...) {
...
});
or 要么
PluginVersion.update({
_id: version._id
}, {
$inc: {
updatedChecks: 1
}
}, function(...) {
...
});
You can find more update operators here: http://docs.mongodb.org/manual/reference/operator/update/ 您可以在此处找到更多更新运算符: http : //docs.mongodb.org/manual/reference/operator/update/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.