如何从 node.js 更新 mongodb 文档?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18565351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:28:29  来源:igfitidea点击:

how to update a mongodb document from node.js?

node.jsmongodb

提问by IrishGringo

I am trying to update an array in a mongoDB from a node.js program. I am able to modify the array from within node.js, but I can not get the changes to save.

我正在尝试从 node.js 程序更新 mongoDB 中的数组。我可以从 node.js 中修改数组,但无法保存更改。

http://pastebin.com/j0Mnf7jP

http://pastebin.com/j0Mnf7jP

I think I am doing something very wrong. assistance would be appreciated...

我想我做错了什么。帮助将不胜感激...

回答by tymeJV

Change this line:

改变这一行:

({_id:doc._id},$set:{scores:zz});

To:

到:

({_id:doc._id}, { $set:{scores:zz}} );

This should also probably be wrapped with a callback, to catch errors:

这也应该用回调包装,以捕获错误:

db.schools.update({_id:doc._id}, {$set:{scores:zz}}, function(err, result) {
    if (err)
        //do something.
});

回答by daemonexmachina

I know it's a bit late to help you now, but maybe others can benefit as new cohorts pass through MongoDB University!

我知道现在帮助你有点晚了,但也许其他人可以随着新的队列通过 MongoDB 大学而受益!

db.schools.updateshould read db.students.update.

db.schools.update应该读db.students.update

@tymeJV's answer gives the rest:

@tymeJV 的回答给出了其余的:

  • Wrap the $setinside braces: {$set:{scores:zz}}
  • Add a callback function to catch errors:

    db.collection( 'students' ).update (
        { _id : doc._id },
        { $set : { scores:zz } },
        function( err, result ) {
            if ( err ) throw err;
        }
    );
    
  • 包裹$set内花括号:{$set:{scores:zz}}
  • 添加回调函数以捕获错误:

    db.collection( 'students' ).update (
        { _id : doc._id },
        { $set : { scores:zz } },
        function( err, result ) {
            if ( err ) throw err;
        }
    );
    

Funnily enough, I'm actually doing exactly the same assignment right now! I had a different issue that was answered by reading the docs, but I saw this question while googling for it. Hope I helped someone!

有趣的是,我现在实际上正在做完全相同的任务!我有一个不同的问题,通过阅读文档得到了回答,但我在谷歌搜索时看到了这个问题。希望我帮助了某人!

回答by Prathamesh Rasam

var dbName = 'school'
var tableName = 'classA'
MongoClient.connect(dbName, function(err, db) {
  if (err)
  {
    console.log(err);
  }
  else {

    var collection = db.collection(tableName)
    collection.update({_id:doc._id}, {$set:{scores:zz}}, function(err, result) {
      if (err)
      {
        console.log(err);

      }
      else{
        console.log(result);
      }
    });
  }
});

回答by Yogesh

I think you should do following code for solving issues

我认为您应该执行以下代码来解决问题

    var lowScore = 9999.9;
        for ( var i=0; i<doc.scores.length; i++ ) {
            if ( doc.scores[i].type == "homework"
               && doc.scores[i].score < lowScore ) {
                    lowScore = doc.scores[i].score;
            }
        }

and then update your collection using following query

然后使用以下查询更新您的收藏

collection.update({ "_id":doc._id }, 
{ $pull : { "scores" : {
$and: [ {"type":"homework"}, { "score":lowScore} ]
} } },
{ "safe":true },
function( err, result ) {
if (err) {
 console.log(err);
  }
} // update callback
); 

for more info you can refer here

有关更多信息,您可以参考 这里