【问题标题】:Update multiple records in a Model with Waterline on Mongodb在 Mongodb 上使用 Waterline 更新模型中的多条记录
【发布时间】:2017-09-25 14:42:31
【问题描述】:

Given 是一个包含书籍对象的数组,其中每个 status 都必须更新:

var books = [ 
              { id: '58b2e58', status: 'pending' },
              { id: '5903703', status: 'pending' } 
                ]

var status = {status: 'accepted'}

我试过this

Book.update(books, status).exec(function afterwards(err, updatedbooks) {
            if (err) {
                res.json(err)
                }
            console.log("updatedbooks:", updatedbooks);
});

但是“updatedbooks”日志是空的。签入mongodb的时候,书就在那里,我可以用Book.find(books);找到它们

作为here mentioned,这在这里工作正常,但我希望在 ID 旁边还有 WHERE 条件中的 status=="pending"。

【问题讨论】:

  • “空”是什么意思?你能提供日志吗?
  • 先生,请... console.log("updatedbooks:", updatedbooks);给出“更新书:[]”
  • 你知道这个操作不是幂等的,对吧?可能是您之前运行了该函数并且已经将对象更改为所需的状态,然后添加了 console.log 并再次运行了该函数,但它不匹配并因此更新了任何内容?这显然会返回任何对象作为Sucessfully Updated Records 回调参数的一部分。安息日快乐!
  • 先生,不,那不是真的。因为在名为“afterwards()”的函数中调用了 console.log,这意味着在更新完成之后。兄弟你还有说明书吗?安息日快乐。
  • 是的,但是如果您已经更新了图书的所有状态字段,那么再次运行该功能时它不会更新任何内容。

标签: node.js mongodb sails.js waterline


【解决方案1】:

请试试这个。

var bookId= ['1','3'];
var status = {status: 'accepted'}
Book.update({id:bookId, status: 'pending'}, status).exec(function afterwards(err, updatedbooks) {
        if (err) {
            console.log('error in update\n'+err);
            res.json(err)
            }
        else{
            console.log("updatedbooks:", updatedbooks);
        }
});

这应该可行。此外,如果您遇到错误,它会让您了解您遇到的错误。

【讨论】:

  • 知道如何将图书 ID 1 的状态更新为已接受,将图书 ID 3 的状态更新为已拒绝?我尝试了以下方法,但两条记录都在更新为接受状态。 var status = {status: ['accepted', 'rejected']} Book.update({id: bookId, status: 'pending'}, status).exec((err, updated) => {})
  • @99darshan 要更新具有不同值的多条记录,您可以使用底层数据库管理器及其本机方法,或者创建一个 Promise.all([])... 集合,其中每个 Promise 都是单独的 Book .update() 提交。 Sails 中没有不同值的批量更新方法。
  • 第一次使用 Book.find。在其中,迭代数据,使用所需值更新所需数据,然后使用 data.save 方法进行更新。保存会自动更新内容。
【解决方案2】:

如果有人有正确答案,请不要等待。我找到了解决方法:

find() 函数可以采用如下条件:

var bookCriteria = [ 
              { id: '1', status: 'pending' },
              { id: '3', status: 'pending' } 
                ]

update() 函数仅适用于具有 ids 的数组:

var bookCriteria = [ '1','3']

因此,让我们首先搜索符合我们条件的书籍,然后获取这些书籍并更新它们。

      Book.find(bookCriteria).exec(function (err, booksFounded) {
            //booksFounded is an array with book objects, with status = 'pending'
            //but we need an array with booksFounded ids as strings

              var bookIDs = booksFounded.map( book => book.id}); //['1','3']

            //now update:

                Book.update(bookIDs , {status:'accepted'}).exec(function afterwards(err, updatedbooks) {
                if (err) {
                    res.json(err)
                    }
                console.log("updatedbooks:", updatedbooks);
                });

   })

您有更好的解决方案吗?使用我的解决方案,它需要 2 个数据库事务,但它可以工作。

【讨论】:

  • 这里 (var bookIDs = booksFounded.map( book => book.id});),您可以使用bookCriteria 代替booksFounded 并摆脱find 呼叫。这对你有用吗?
  • 不,因为就像我写的那样,我只需要更新具有status:pending 的书籍。 find() 函数可以采用 bookCriteria,但 update() 函数不能。这就是为什么,你首先必须“过滤”那些有ID 的书是pending。之后更新满足bookCriteria 的新成立的书籍。 - 谁对答案投了反对票,却没有更好的答案??
猜你喜欢
  • 1970-01-01
  • 2017-09-03
  • 2013-01-21
  • 2011-06-19
  • 1970-01-01
  • 2020-04-25
  • 2019-05-23
  • 1970-01-01
  • 2011-07-15
相关资源
最近更新 更多