【问题标题】:How to insert a document in MongoDB stitch with rule enabled on a field如何在 MongoDB 缝合中插入文档并在字段上启用规则
【发布时间】:2018-02-27 00:15:11
【问题描述】:

我正在尝试新的 MongoDb 拼接,但无法在字段上定义规则。

当我尝试在任何字段上没有规则或仅在顶级文档中插入它收集的文档时,它可以正常插入。 但是,只要我添加一个字段并编写一条规则,它就会给出错误“无法写入文档”。即使规则是“{}”,它总是评估为真。 下面是一个示例插入管道和定义的规则。

顶级文件: R {} W“空白” 领域: 姓名 W {}

插入管道 第一阶段:

Service: "built-in", Action "literal", 
args:"{
  "items": [
    {
      "name": "Don Giovanni",
      "cuisine": "Italian",
      "location": "Chicago, IL",
      "comments": [
        {
          "user_id": "leporello",
          "comment": "Molto bene"
        },
        {
          "user_id": "59b256814fdd1f75d5e1dce3",
          "comment": "insertion"
        },
        {
          "user_id": "59b256814fdd1f75d5e1dce3",
          "comment": "good food"
        },
        {
          "user_id": "59b256814fdd1f75d5e1dce3",
          "comment": "very good food"
        },
        {
          "user_id": "59b256814fdd1f75d5e1dce3",
          "comment": "insertion"
        }
      ]
    }
  ]
}"

Stage 2 :
Service :"mongodb-atlas", Action:"insert",database:"guidebook", collection:"restaurants"

【问题讨论】:

    标签: android mongodb mbaas


    【解决方案1】:

    如果您在顶级文档中的写入规则为空,Stitch 将尝试为适用于您尝试插入的文档的每个字段/数组/子文档查找可写入规则。

    在您的情况下,为name 字段设置一个全部写入(即{})规则是不够的,您需要为文档中存在的每个其他字段设置一个规则(即@ 987654326@、locationcuisinecomments),如下图所示。请注意,您必须将 comments 字段声明为 Array 而不是 Any 才能使插入工作。

    您可以在 Stitch 文档中找到有关 Stitch 规则交互的更多信息:https://docs.mongodb.com/stitch/rules/mongodb-rules/#mongodb-service-rules-interaction

    【讨论】:

    • 我的问题是我没有为“所有其他字段”写 write 并将其留空。只要我为它写了一个规则,它就可以正常工作。
    猜你喜欢
    • 2020-01-09
    • 1970-01-01
    • 2019-10-31
    • 2020-09-08
    • 1970-01-01
    • 2017-01-31
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多