8

I have a document as below, and I want to pull all the elements in this array without any condition just via one statement. how can I do?

    "energy_sent" : [ 
        {
            "player_id" : "034010000093",
            "_id" : ObjectId("53675b8d251c20490d9679c6"),
            "time" : ISODate("2014-05-05T09:36:13.629Z"),
            "has_accepted" : 0,
            "energy_value" : 2
        }, 
        {
            "player_id" : "034010000094",
            "_id" : ObjectId("53675cfa251c20490d9679cc"),
            "time" : ISODate("2014-05-05T09:42:18.015Z"),
            "has_accepted" : 0,
            "energy_value" : 2
        }, 
        {
            "player_id" : "034010000116",
            "_id" : ObjectId("5367767889f8e3ee137dd239"),
            "time" : ISODate("2014-05-05T11:31:04.457Z"),
            "has_accepted" : 0,
            "energy_value" : 2
        }
    ]
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Hunter Zhao
  • 4,589
  • 2
  • 25
  • 39

1 Answers1

15

If you are just after emptying the entire array just set it to empty:

db.collection.update(
   { /* query to match document */ },
   { "$set": { "energy_sent": [] }
)

So just use the $set operator

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317