如何在没有任何条件的情况下从MongoDB中的数组中提取所有元素

Hun*_*hao 4 mongoose mongodb node.js mongodb-query

我有一个如下文档,我想通过一个语句拉出这个数组中的所有元素,没有任何条件.我能怎么做?

    "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
        }
    ]
Run Code Online (Sandbox Code Playgroud)

Nei*_*unn 7

如果你刚刚清空整个数组后只需将其设置为空:

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

所以只需使用$set运营商即可