0

Don't think this is at all possible but wanted to check.

If I have the following documents:

doc1 : {
  _id: 1,
  name: 'John Smith',
  age: 20
}

doc2 : {
  _id: 2,
  name: 'Jane Smith',
  age: 22
}

I want the client to be able to pass me both docs in the same request for update. In this case maybe add an address to both docs.

Is there a way the I can send one update statement to mongo such that it updates both of the documents with the name values?

I.E. from client:

doc1 : {
  _id: 1,
  name: 'John Smith',
  age: 20,
  address: '123 Street'
}

doc2 : {
  _id: 2,
  name: 'Jane Smith',
  age: 22,
  address: '456 Way'
}

Currently I am iterating over the values and updating one at a time. Problem with that is mongoose/mongodb updates are async, so I cannot reliably tell the client that I updated each result until all update callbacks have fired. I have a counter to make sure I receive N number of callbacks then I send a response.

2
  • Splitting it into two updates is required. Take a look at the async library for ways to improve on the callback counter approach. Commented Mar 28, 2013 at 15:20
  • If you were applying the same update to multiple documents, you could use the multi:true flag for update(). Commented Apr 3, 2013 at 1:12

1 Answer 1

0

All the updates I do are one at a time, but what's nice for me is that they are synchronous. =)

It seems you can bulk load though with the mongoimport tool: http://docs.mongodb.org/manual/reference/mongoimport/#cmdoption-mongoimport--upsertFields. You can do bulk upserts that way.

Otherwise I don't believe you can do it in one command via a client.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.