MongoError:未知修饰符:节点 js 中的 $pushAll

新手上路,请多包涵

我在保存模型失败并 出现 mongo 错误时遇到问题:MongoError: Unknown modifier: $pushAll

我的模式中有一个数组字段 subDomains ,它将被保存为子域的默认值,如下所示。

  // already Domain instance get availble
 Domain.subDomains.push({'name': 'default' , 'role': 'xyz', ...});

 // save domain with default fileds
 Domain.save()

系统信息如下:

          ➜  ~ node --version
          v9.4.0
          ➜  ~ npm --version
          5.6.0
          ➜  ~
          ➜  ~ mongo --version
            MongoDB shell version v3.6.2
            git version: ......
            OpenSSL version: OpenSSL 1.0.2n  7 Dec 2017
            allocator: system
            modules: none
            build environment:
                distarch: x86_64
                target_arch: x86_64
          ➜  ~

请帮我解决这个问题。

原文由 Santosh Shinde 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 636
2 个回答

Mongo 3.6.2(或 3.6.x+ 的任何更新版本)不再支持 $pushAll 运算符。

您可以执行以下操作:

   new mongoose.Schema({
    username: String
  }, {
    usePushEach: true
  });

  • 降级到 Mongo 3.4.11(或任何 3.4.x 版本)

原文由 Teodor Kurtev 发布,翻译遵循 CC BY-SA 4.0 许可协议

使用以下代码添加到上述答案中,以使您清楚地了解 concat 方法的实现。您不需要特别降级 MongoDB。

在解决此问题之前,您需要记住 $pushAll 方法已被弃用很长时间,因此他们可能会在 3.5 及更高版本中删除它。

但是您可以使用另一种方法 ——concat 方法来解决这个问题。

我的旧代码,

 myArray.push(id); //breaks on DocumentDB with Mongo API because of $pushAll

已被替换为,

 myArray = myArray.concat([id]); //this uses $set so no problems

甚至 Azure 自己的 MongoDB 实现 也不支持 $pushAll。

谢谢你。

原文由 Balasubramani M 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题