【问题标题】:How to create nested array in realm without key(React Native)如何在没有键的领域中创建嵌套数组(React Native)
【发布时间】:2020-12-08 05:35:50
【问题描述】:
{
    "a": [
        [
            {
              "_id": "57e55b64016c3551c025abc1",
              "title": "Main Campus"
            },
            {
              "_id": "5810e2e27064497f74ad4874",
              "title": "Ahm Campus"
            },
            {
              "_id": "5d5d2633a1d0680620ac3cce",
              "title": "Baroda"
            },
            {
              "_id": "5d5d3af3a1d0680620ac3ef8",
              "title": "India"
            }
          ],
          [
            {
              "_id": "57e55b64016c3551c025abc1",
              "title": "Main Campus"
            },
            {
              "_id": "5810e2e27064497f74ad4874",
              "title": "Ahm Campus"
            },
            {
              "_id": "5d5d2633a1d0680620ac3cce",
              "title": "Baroda"
            },
            {
              "_id": "5d5d3af3a1d0680620ac3ef8",
              "title": "India"
            }
          ]

    ]
  }

如何在领域(React Native)中为这种类型的 JSON 对象创建模式。我尝试了所有可能的方法,但没有找到任何具体的解决方案。基本上,它是一个嵌套数组,其中第二个数组没有任何特定的键(我尝试使用键它工作正常,但我想在不添加键的情况下这样做)。

【问题讨论】:

    标签: reactjs mobile realm native


    【解决方案1】:

    你可以使用类似的东西:

    const ParentSchema = {
      name: "parent",
      properties: {
        key: "string",
        values: "Value[]"
      }
    };
    
    const ValueSchema = {
      name: "Value",
      embedded: true,
      properties: {
        _id: "string",
        title: "string"
      }
    };
    

    您可以插入如下对象:

    realm.write(() => {
      realm.create("Parent", { key: "a", values: [
          { _id: "57e55b64016c3551c025abc1", title: "Main Campus" },
          { _id: "5810e2e27064497f74ad4874", title: "Ahm Campus" }
        ]
      });
    });
    

    文档:https://docs.mongodb.com/realm/node/data-model

    【讨论】:

    • 嘿@geisshirt 感谢您的回答,但我不想添加“价值”是否可以在没有价值的情况下做同样的事情?我想推送与我发布的问题相同的 JSON 数据
    • 目前你不能,但github.com/realm/realm-js/issues/3392 可能有你的兴趣。
    【解决方案2】:

    到目前为止,没有键无法在 Realm 数据库中直接插入值,所以现在我们需要修改数据,然后我们可以存储在以下模式中。

    const ParentSchema = {
      name: "parent",
      properties: {
       a: "level[]"
      }
    };
    
    const level = {
        name: 'level',
        properties: {
            level: 'sites[]'
        }
    }
    const sites = {
        name: 'sites',
        properties: {
            sites: 'site[]'
        }
    }
    const site = {
        name: 'site',
        properties: {
    
            title: 'string?',
            _id: 'string?',
            version: 'int?',
        }
    }
    

    数据修改需要如下进行。

    var a = {
        level: []
     }
    
    data.a.map((Site, index) => {
             const sites = []
             Site.map((s) => { sites.push(s)})
             a.level.push({sites})
     })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      • 1970-01-01
      • 2022-06-22
      • 1970-01-01
      • 2020-10-20
      • 2021-10-09
      • 1970-01-01
      相关资源
      最近更新 更多