Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 猫鼬赢了';更新子文档_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js 猫鼬赢了';更新子文档

Node.js 猫鼬赢了';更新子文档,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,几个小时以来,我一直在为这个问题绞尽脑汁,但似乎无法理解。我决定在一个学习项目中使用Mongo,看看我有多喜欢它。猫鼬是推荐的使用方法,所以我也加入了。我有两种模式: var PluginSchema = new Schema({ name: { type: String, required: true }, slug: { type: String }, description: { type: String }, created_date: { type: D

几个小时以来,我一直在为这个问题绞尽脑汁,但似乎无法理解。我决定在一个学习项目中使用Mongo,看看我有多喜欢它。猫鼬是推荐的使用方法,所以我也加入了。我有两种模式:

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);
    }
};

这里的问题是“版本”关系。在某个时候,我想更新插件的一个版本。我要更新的是
updatedChecks
字段。基本上就是
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.");
                }
            });
        }
    });

到目前为止,一切顺利。然而,当我试图通过插件模式再次访问该版本时,
updatedChecks
值没有改变!我检查了我正在更新的版本上的
\u id
值与从
插件中提取的版本。versions
字段中的版本相同。是否需要从
Plugin.versions
中删除该版本,然后使用更新后的值重新插入一个新版本?我还尝试了更新值并调用
save()
,但这似乎也不起作用。

这里的问题是,您的更新查询写错了。一定是

PluginVersion.update({
    _id: version._id
}, {
    $set: {
        updatedChecks: version.updatedChecks + 1
    } 
}, function(...) {
    ...
});

您可以在此处找到更多更新运算符:

我通过直接从插件对象访问插件版本来完成这项工作

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);
        });
    });
}
var-pluginIndex=false;
对于(var i=0;i

我还得到了一些帮助:

作为另一个提示,这看起来有点符合SQLy的口味。如果您在其他模式中共享PluginVersion,则它可能位于单独的模块中,但即使在这种情况下,也不应是单独的集合。然后更新也更容易。否则,如果MongoDB优于SQL,您将失去一个。
PluginVersion.update({
    _id: version._id
}, {
    $inc: {
        updatedChecks: 1
    } 
}, function(...) {
    ...
});
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);
        });
    });
}