简体   繁体   English

如何在字段上启用规则的情况下在 MongoDB 缝合中插入文档

[英]How to insert a document in MongoDB stitch with rule enabled on a field

I am trying the new MongoDb stitch but cannot work out with defining rules on fields.我正在尝试新的 MongoDb 针法,但无法在字段上定义规则。

When I try to insert a document it collection with no rules on any Fields or only in Top level document it inserts fine.当我尝试插入一个文档时,它在任何字段上都没有规则或仅在顶级文档中插入它可以正常插入。 But as soon as I add a Field an write a rule it gives error "document cannot be written to".但是,一旦我添加了一个字段并写入了一条规则,它就会出现错误“无法写入文档”。 Even when the rule is "{}" which always evaluates to true.即使规则是“{}”,它总是评估为真。 Below is a sample insert Pipeline and defined rule.下面是一个示例插入管道和定义的规则。

TOP LEVEL DOCUMENT: R {} W "blank" FIELDS : name W {}顶级文档:R {} W“空白”字段:名称 W {}

Insert Pipeline Stage 1 :插入管道阶段 1 :

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"

If your write rule at the top-level document is blank, Stitch will try to find a writeable rule for each of the fields/arrays/sub-documents that would apply to the document you're trying to insert.如果顶级文档中的写入规则为空,Stitch 将尝试为适用于您尝试插入的文档的每个字段/数组/子文档查找可写入规则。

In your case, it's not enough to have a write all (ie {} ) rule for the name field, you need one for each other field present in your document (ie _id , location , cuisine and comments ), as shown in the picture below.在您的情况下,为name字段设置全部写入(即{} )规则是不够的,您需要为文档中的每个其他字段(即_idlocationcuisinecomments )设置一个规则,如图所示下面。 Note that you must declare the comments field as Array not Any for your insert to work.请注意,您必须将comments字段声明为Array而不是Any才能使插入工作。

You can find out more about the Stitch rules interaction in the Stitch documentation: https://docs.mongodb.com/stitch/rules/mongodb-rules/#mongodb-service-rules-interaction您可以在 Stitch 文档中找到有关 Stitch 规则交互的更多信息: https : //docs.mongodb.com/stitch/rules/mongodb-rules/#mongodb-service-rules-interaction

缝合规则

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM