Mongodb. Convert field to bool

Posted on

Question :

I’m trying to change field type to bool among all documents in collection. Here is the code

db.product.find().forEach(function(doc) {
if (doc.checked === "Y") {
doc.checked = true;
} else if (doc.checked === "N") {
doc.checked = false;
}
db.product.save(doc);
});

Mongodb returns no error but doesn’t convert fields.

Answer :

I solved problem using ‘db.product.update’ instead of ‘db.product.save’

Leave a Reply

Your email address will not be published. Required fields are marked *