How to do bulkWrite in mongodb using nodejs driver to insert element to an array while keeping the recent 100 records only - Sta

admin2025-04-27  3

I want to update an array field of a mongodb document, pushing a new value into the array, and keeping only the recent 100 values, and do it as bulkWrite operation

According to official docs we can do

db.students.updateOne(
   { _id: 1 },
   {
     $push: {
       scores: {
         $each: [ 80, 78, 86 ],
         $slice: -5
       }
     }
   }
)

for single update operation, however when I try to do it using bulkWrite it gives syntax error (unexpected token ':' after $slice.

updateOne: {
        filter: { name: coin.name }
        update: {
          $push: {
            price_history: {
              $each: [coin.current_price_inr], 
              $slice: -100
            }
          }
        }
      
转载请注明原文地址:http://anycun.com/QandA/1745710667a91151.html