【问题标题】:How to pull all elements from array in MongoDB without any condition如何在没有任何条件的情况下从MongoDB中的数组中提取所有元素
【发布时间】:2014-06-22 23:27:16
【问题描述】:

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

    "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
        }
    ]

【问题讨论】:

  • “拉所有”对您意味着什么?你想要一个空数组作为结果吗?
  • 是的,我只想要一个空数组,你知道吗?

标签: node.js mongodb mongoose mongodb-query


【解决方案1】:

如果您只是在清空整个数组后将其设置为空:

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

所以只需使用$set 运算符

【讨论】:

  • 多么棒的方法,它对我有用,非常感谢:) @Neil Lunn
猜你喜欢
  • 2021-12-05
  • 1970-01-01
  • 2020-07-10
  • 1970-01-01
  • 2011-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-27
相关资源
最近更新 更多