0
{
    "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"
            }
          ]

    ]
  }

How to create the schema in the realm(React native) for this type of JSON object. I tried all possible ways but did not found any specific solution. Basically, it is a nested array where the second array does not have any specific key(I tried with key it works fine but I want to do it without adding key).

2 Answers 2

0

You can use something like:

const ParentSchema = {
  name: "parent",
  properties: {
    key: "string",
    values: "Value[]"
  }
};

const ValueSchema = {
  name: "Value",
  embedded: true,
  properties: {
    _id: "string",
    title: "string"
  }
};

You can insert objects like:

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

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

2
  • Hey @geisshirt Thanks for the answer but I dont want to add "value" is it possible to do the same without having value? I want to push JSON data the same as I have posted in question Commented Dec 8, 2020 at 14:27
  • Currently you can't but github.com/realm/realm-js/issues/3392 might have your interest.
    – geisshirt
    Commented Dec 8, 2020 at 19:56
0

As of now there is no way to insert direct value in Realm database without key so for now we need to modify data and then we can store in following schema.

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?',
    }
}

Data modification need to done like following.

var a = {
    level: []
 }

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.