Home > Blockchain >  How to delete nested element from mongo db based on conditon
How to delete nested element from mongo db based on conditon

Time:05-10

{ "_id":62749de1c511aff4354802f0 "filename":"anomaly-detection-using-amazon.pdf" "tags":[ {'entity': 'Aws', 'count': 21}, {'entity': 'Keras', 'count': 1}, {'entity': 'Amazon Ml', 'count': 1}, {'entity': 'Amazon Cloudwatch', 'count': 1} ] }

if value of entity string length less than 3 then output is

{ "_id":62749de1c511aff4354802f0 "filename":"anomaly-detection-using-amazon.pdf" "tags":[ {'entity': 'Keras', 'count': 1}, {'entity': 'Amazon Ml', 'count': 1}, {'entity': 'Amazon Cloudwatch', 'count': 1} ] }

This my document where i want to delete an entity key from tags array if length of the key value as string is less than 3

CodePudding user response:

Try this one:

db.collection.aggregate([
   {
      $set: {
         tags: {
            $filter: {
               input: "$tags",
               cond: { $gt: [{ $strLenCP: "$$this.entity" }, 3] }
            }
         }
      }
   }
])

Mongo Playground

  • Related