如何在没有键的情况下在领域中创建嵌套数组(React Native)

How to create nested array in realm without key(React Native)

{
    "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 对象创建模式。我尝试了所有可能的方法,但没有找到任何具体的解决方案。基本上,它是一个嵌套数组,其中第二个数组没有任何特定的键(我尝试使用键它工作正常但我想在不添加键的情况下这样做)。

您可以使用类似的东西:

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

到目前为止,没有办法在没有键的情况下直接在 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})
 })