I would like to shorten all long descriptions in my object. I have searched and read many articles but I can't figure out how to do this simple task in mongoDB. What I am trying to achieve would be simple in SQL:
UPDATE AssetDocument SET description = substr(description, 0, 500) WHERE length(description) > 500
Can please someone help me do this in MongoDB?
I have tried this:
db.AssetDocument.updateMany(
{$where: "this.metadata.description.length > 500"},
{$set: { "metadata.description": { $substr: ["metadata.description", 0, 500]}}});
This gives me errmsg: The dollar ($) prefixed field '$substr' in 'metadata.description.$substr' is not valid for storage.
Then I tried this:
db.AssetDocument.find({
$where: "this.metadata.description.length > 500"
}).forEach(function(e){
e.metadata.description = {$substr: [e.metadata.description, 0, 500]};
db.AssetDocument.save(e);
});
But this doesn't work...