List<BSONObject> updates = (List<BSONObject>) query.get("updates");
int n = 0;
boolean updatedExisting = false;
Collection<BSONObject> upserts = new ArrayList<BSONObject>();
for (BSONObject updateObj : updates) {
BSONObject selector = (BSONObject) updateObj.get("q");
BSONObject update = (BSONObject) updateObj.get("u");
boolean multi = Utils.isTrue(updateObj.get("multi"));
boolean upsert = Utils.isTrue(updateObj.get("upsert"));
final BSONObject result = updateDocuments(channel, collectionName, selector, update, multi, upsert);
updatedExisting |= Utils.isTrue(result.get("updatedExisting"));
if (result.containsField("upserted")) {
final Object id = result.get("upserted");
final BSONObject upserted = new BasicBSONObject("index", upserts.size());
upserted.put("_id", id);
upserts.add(upserted);
}
n += ((Integer) result.get("n")).intValue();
}
BSONObject response = new BasicBSONObject("n", Integer.valueOf(n));
response.put("updatedExisting", Boolean.valueOf(updatedExisting));
if (!upserts.isEmpty()) {
response.put("upserted", upserts);
}
Utils.markOkay(response);
putLastResult(channel, response);
return response;
}