Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I'm new to Mongo. I need to iterate documents in a query result, update a single field and save the document.

My understanding is that I need to iterate the query result with a cursor, get the record's _id, and then use that in the search query that is passed in the call to update();

The code below runs but the values never make it back to the database. I would be extremely grateful for any assistance. Thanks in advance.

I'm running a default out-of-the-box Mongo instance.

Here is the chunk of code that isn't working:


byte[] outputFile = new byte[10];
outputFile[0] = 40;

BasicDBObject newDocument = new BasicDBObject();

newDocument.append("$set", new BasicDBObject().append("SomeField", outputFile));

String id = cursor.curr().get("_id").toString();

BasicDBObject searchQuery = new BasicDBObject().append("_id", id);

fileCollection.update(searchQuery, newDocument, true, false);
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900