I am going to assume that you're working directly inside the mongo shell.
Once you have your document (not a row :P ), you'd modify the properties in the same way you would a normal JavaScript object:
var doc = db.barfoo.findOne( { "name": "bar" } );
doc.name = "Mr Bar";
Note that the find()
command returns a cursor, so if you're looking to extract a single document, you should use the findOne()
function. This function returns a single document.
If you are interested in duplicating numerous documents, you can use the find()
function and iterate over the cursor to retrieve each document:
db.barfoo.find( { "name": "bar" } ).forEach( function( doc ){
doc.name = "Mr Bar";
}
After you change the relevant properties, you can use the insert/save
methods to persist the data back to mongo. Don't forget to change/delete the _id
attribute so that you'll actually create a new document.
As a side note, in order to view the contents of an object in the mongo shell, you can use the print()
function. If you want a more visually appealing output, you could use printjson()
.