在nedb中更新一行 [英] Update a row in nedb

查看:147
本文介绍了在nedb中更新一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在nedb中有以下数据.

I have the following data in nedb.

["UserId":"1446943507761","UserName":"xxx","link":"xxx.html","taskDone":"false","id":14,"_id":"fdaaTWSxloQZdYlT"]
["UserId":"1446943507761","UserName":"xxx","link":"xxx.html","taskDone":"false","id":1,"_id":"fzh2cedAXxT76GwB"]
["UserId":"1446943507761","UserName":"xxx","link":"xxx.html","taskDone":"false","id":0,"_id":"k4loE7XR5gioQk54"]

我正在尝试更新ID为0的行并将taskDone的值设置为true.我使用以下查询将值设置为true

I am trying to update row with id 0 and set the value of taskDone to true. I use the following query to set the value to true

db.taskmap.update({ _id: "k4loE7XR5gioQk54", UserName:"xxx" }, { $set: { taskDone: "true"} }, function (err, numReplaced) {
    console.log("replaced---->" + numReplaced);
  });

它会更新值,但会更新为新行.它基本上会插入一个具有相同值的新行,但taskdone值为true.它不会删除现有数据.因此,在更新后的最终数据表中,我获得了ID为0的两个行,除了taskDone以外,其他所有值都相同.我不确定我做错了什么.如果有人可以告诉我正确的值更新方法,将会很有帮助.

It updates the value but it updates as a new row. It basically inserts a new row with same values except for the taskdone value as true. It does not delete the existing data. Hence in the final data table after update i get tow rows for id 0 with all values same except for the taskDone. I am not sure if i am doing anything wrong. It will be helpful if anybody can tell me a correct way of updating the value.

推荐答案

update想要四个参数

var Datastore = require('nedb');
var db = new Datastore();

db.insert(
[
  {
    "UserId":"1446943507761",
    "UserName":"xxx",
    "link":"xxx.html",
    "taskDone":"false",
    "id":14,
    "_id":"fdaaTWSxloQZdYlT"
  },
 {
    "UserId":"1446943507761",
    "UserName":"xxx",
    "link":"xxx.html",
    "taskDone":"false",
    "id":1,
    "_id":"fzh2cedAXxT76GwB"
 },
 {
    "UserId":"1446943507761",
    "UserName":"xxx",
    "link":"xxx.html",
    "taskDone":"false",
    "id":0,
    "_id":"k4loE7XR5gioQk54"
  }], 
  function (err, newDocs) {
    // empty here
  }
  );
db.update(
           { _id: "k4loE7XR5gioQk54", UserName:"xxx" }, 
           { $set: { taskDone: "true"} },
           {},// this argument was missing
           function (err, numReplaced) {
             console.log("replaced---->" + numReplaced);
           }
           );
// should give the correct result now
db.find({}).exec(function (err, docs) {console.log(docs);});

这篇关于在nedb中更新一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆