0

我在一个节点项目中有这个功能,它应该根据 slug 是否匹配来更新或创建一个新条目。它更新完美,因此返回的响应是正确的,但它不会插入新项目,我不确定我错过了什么。

MongoDB 版本 3.0.4(通过 Homebrew 安装) Mac OSX Yosemite MongoDB 节点包 2.0.39 Monk 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");
        }
    });

我在这里错过了一些非常明显的东西吗?

4

4 回答 4

2

我在这里找到了正确的答案:简单的 mongo/monk findAndModify 查询的问题

解决方案是将选项作为自己的对象提供(僧侣文档没有很好地记录它):

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);
    }
});
于 2015-11-06T08:57:07.093 回答
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);
        }
    });
于 2015-07-17T16:37:19.403 回答
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 方法文档应该会有所帮助。

于 2016-08-25T00:15:51.547 回答
-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}
        );
    });
});
于 2015-10-08T13:24:11.267 回答