为什么.save在Mongoose中工作,而不是带有upsert的.update

时间:2022-02-23 02:37:37

My save is:

我的保存是:

        tx = new Transaction transaction

        tx.save (err) ->
            console.log err
            cb err

That saves just fine.

这样可以节省很多。

    Transaction.update transaction, {upsert: true}, (err, num, raw) ->
      console.log err
      console.log num
      console.log raw

However, my update won't upsert a document. Furthermore, it returns no err and 0 for num. This is with "mongoose": "~3.8.0"

但是,我的更新不会包含文档。此外,它没有返回错误,0表示num。这是“猫鼬”:“~3.8.0”

1 个解决方案

#1


1  

The first parameter to the update function is the query to match one or more documents (reference).

update函数的第一个参数是匹配一个或多个文档的参数(引用)。

I'd expect the call to Transaction.update to only include the document's _id, rather than the entire object. The second parameter is either the entire document or an object using update operators.

我希望对Transaction.update的调用只包含文档的_id,而不是整个对象。第二个参数是整个文档或使用更新运算符的对象。

It's possible you want:

你可能想要:

Transaction.update _id : transaction._id, transaction, upsert: true, 
   (err, num, raw)->    

Or, maybe you want to set specific properties of that transaction using one of the documented update operators.

或者,您可能希望使用其中一个记录的更新运算符设置该事务的特定属性。

Transaction.update  _id : transaction._id,  $set :  { amount : 500 } ,
      upsert : true, (err, num, raw) ->

#1


1  

The first parameter to the update function is the query to match one or more documents (reference).

update函数的第一个参数是匹配一个或多个文档的参数(引用)。

I'd expect the call to Transaction.update to only include the document's _id, rather than the entire object. The second parameter is either the entire document or an object using update operators.

我希望对Transaction.update的调用只包含文档的_id,而不是整个对象。第二个参数是整个文档或使用更新运算符的对象。

It's possible you want:

你可能想要:

Transaction.update _id : transaction._id, transaction, upsert: true, 
   (err, num, raw)->    

Or, maybe you want to set specific properties of that transaction using one of the documented update operators.

或者,您可能希望使用其中一个记录的更新运算符设置该事务的特定属性。

Transaction.update  _id : transaction._id,  $set :  { amount : 500 } ,
      upsert : true, (err, num, raw) ->