1

If I have the ids for the following documents that belongs to a big collection and a newBusinessId to update those documents. Something like this:

const newBusinessId = '8abc4cef176aa342154d00c0'
const paymentsIds = ['5aba5cef176aa342154d2345', '5aba5cef176aa342154d6789']

And those ids belong to the following documents in my database:

[
  {
    _id: '5aba5cef176aa342154d2345',
    state: 'MATURE',
    comment: 'some comment 1',
    startComment: 'some different comment 1',
    expense: 2,
    startExpense: 2,
    businessId: '5aba5cef176aa342154d9876'
  },
  {
    _id: '5aba5cef176aa342154d6789',
    state: 'MATURE',
    comment: 'some comment 2',
    startComment: 'some comment 2',
    expense: 3,
    startExpense: 1,
    businessId: '5aba5cef176aa342154d5432'
  },
]

I want to update those documents with the same businessId:newBusinessId, with the state value of INIT and the fields (comment, expense) to be set with the values of (startComment, startExpense) so I can have the following result after updating:

[
  {
    _id: '5aba5cef176aa342154d2345',
    state: 'INIT',
    comment: 'some different comment 1',
    startComment: 'some different comment 1',
    expense: 2,
    startExpense: 2,
    businessId: '8abc4cef176aa342154d00c0'
  },
  {
    _id: '5aba5cef176aa342154d6789',
    state: 'INIT',
    comment: 'some comment 2',
    startComment: 'some comment 2',
    expense: 1,
    startExpense: 1,
    businessId: '8abc4cef176aa342154d00c0'
  },
]

Any idea how? I appreciate your toughts!

Leonardo Uribe
  • 123
  • 1
  • 13
  • Thanks for your answer @Dipak chavda But with that update we're just updating the businessId , right? we're not updating the comment, the expense or the state... That's the problem :/ – Leonardo Uribe Mar 27 '18 at 16:21
  • you mean to say have state value init also update the document right – Dipak Mar 27 '18 at 16:28

1 Answers1

0

in order to update a field with another field's value in the same object ( expense = startExpense for example ), you have to iterate through, try this :

yourModel.find().forEach(
    function (elem) {
        yourModel.update(
            {
                _id: { $in: paymentsIds }
            },
            {
                $set: {
                    state : 'INIT',
                    comment : elem.startComment,
                    expense : elem.startExpense,
                    BusinessId : newBusinessId
                }
            }
        );
    }
);
Taki
  • 17,320
  • 4
  • 26
  • 47
  • Thanks @Taki for your answer! I really appeciate! I've tryied to integrate your solution into my code but i'm getting the following error: `error: TypeError: this.find(...).snapshot(...).forEach is not a function` Any idea why? It seems The snapshot() method is not returning an array. Is that so? Thanks again! – Leonardo Uribe Mar 27 '18 at 18:41
  • try without it `find().forEach()...` as it's useful only to prevent the cursor from returning a document more than once see : https://docs.mongodb.com/v3.4/reference/operator/meta/snapshot/ – Taki Mar 27 '18 at 18:44