我在一个节点项目中有这个函数,它应该根据slug是否匹配来更新或创建一个新条目。它完美更新,因此返回的响应是正确的,但它不会插入新项目,而且我不确定我错过了什么。
MongoDB 3.0.4版(通过Homebrew安装) Mac OSX Yosemite MongoDB节点包2.0.39 僧侣1.0.1
collection.findAndModify({
"query": { "slug": pluginslug },
"update":
{
"slug" : pluginslug,
"name" : response.data.name,
"current_version" : response.data.version,
"description" : response.data.short_description,
"change_log" : response.data.sections.changelog,
"updated" : response.data.last_updated,
"tested" : response.data.tested
},
"upsert" :true,
"new" : true
}, function (err, doc) {
if (err) {
res.send("There was a problem adding the information to the database.");
}
else {
console.log(doc)
res.redirect("plugins/new");
}
});
我错过了一些非常明显的东西吗?
答案 0 :(得分:2)
我在这里找到了正确答案:Issue with a simple mongo/monk findAndModify query
解决方案是将选项作为自己的对象提供(monk docs没有很好地记录):
db.test.findAndModify(
{
"query": { "slug": "1234512345" },
"update":
{
"$set":
{
"slug" : "1234512345",
"name" : "response.data.name",
"current_version" : "response.data.version",
"description" : "response.data.short_description",
"change_log" : "response.data.sections.changelog",
"updated" : "response.data.last_updated",
"tested" : "response.data.teste"
}
}
},
{
"upsert" :true,
"new" : true
},
function (err, doc) {
if (err) {
console.log(err);
}
else {
console.log(doc);
}
});
答案 1 :(得分:1)
试试这个:
collection.findAndModify({
"query": { "slug": pluginslug },
"update":
{
$set:{
"slug" : pluginslug,
"name" : response.data.name,
"current_version" : response.data.version,
"description" : response.data.short_description,
"change_log" : response.data.sections.changelog,
"updated" : response.data.last_updated,
"tested" : response.data.tested
}
},
"upsert" :true,
"new" : true
}, function (err, doc) {
if (err) {
res.send("There was a problem adding the information to the database.");
}
else {
console.log(doc)
res.redirect("plugins/new");
}
});
尝试运行此代码:
"use strict";
var mongojs = require("mongojs");
var collections = ["test"];
var db = mongojs('test', collections);
db.on('error', console.error.bind(console, 'connection error:'));
db.test.findAndModify({
"query": { "slug": "1234512345" },
"update":
{
"slug" : "1234512345",
"name" : "response.data.name",
"current_version" : "response.data.version",
"description" : "response.data.short_description",
"change_log" : "response.data.sections.changelog",
"updated" : "response.data.last_updated",
"tested" : "response.data.teste"
},
"upsert" :true,
"new" : true
}, function (err, doc) {
if (err) {
console.log(err);
}
else {
console.log(doc);
}
});
答案 2 :(得分:1)
这个答案可能为时已晚,但我偶然发现了这个问题并遇到了类似问题,这对我有用:
collection.findAndModify(
{ "slug": pluginslug }
,[]
,{
$set:{
"slug" : pluginslug,
"name" : response.data.name,
"current_version" : response.data.version,
"description" : response.data.short_description,
"change_log" : response.data.sections.changelog,
"updated" : response.data.last_updated,
"tested" : response.data.tested
}
}
,{"upsert" :true,"new" : true}
, function (err, doc) {
//..something here...
});
findAndModify方法文档http://codepen.io/pixelacorn/pen/RROwBL?editors=0100应该有所帮助。
答案 3 :(得分:-1)
尝试下面的想法
router.post('/login/:id', jsonParser, function (req, res) {
var db = req.db;
var collection = db.get('user');
console.log(req.body);
collection.findAndModify({userid : req.params.id}, { $set: req.body } , {upsert : true}, function (err, result) {
console.log(err);
res.send(
(err === null) ? {msg: 'success'} : {msg: 'error: ' + err}
);
});
});