0

I am executing the following:

db.emails.aggregate(
 [
    {$addFields : {arr : {$objectToArray : "$$ROOT"}}}, 
    {$project : { pass : {$slice : ["$arr.v", 1, 20 ] }}}
]
).pretty()

When I exit the session / shell, the changes are not saved.

Can someone please direct me on how to apply this modification to the entire collection and save it?

endrix
  • 1

1 Answers1

0

looks like continuation of How to merge multiple fields in a collection?

you can $out to new collection,check data,rename to old collection name dropping old data

db.emails.aggregate(
 [
    {$addFields : {arr : {$objectToArray : "$$ROOT"}}}, 
    {$project : { pass : {$slice : ["$arr.v", 1, 20 ] }}},
    {$out : "new_emails"} // out to new collection
]
).pretty()

db.new_emails.renameCollection('emails', true); // rename collection, true - dropTarget
Saravana
  • 12,647
  • 2
  • 39
  • 57