praneybehl
praneybehl

Reputation: 3981

Meteor Simple-Schema Collection2 help needed for build nested schema

I am trying to build a collection schema with Meteor Collection2.

Possible schema for my collection is :

{
 items: {
     id: Meteor.ObjectID
     title: String,
     Desc: String,
     createdAt: new Date(),
     creator: String,
     invitedUsers: [String],
     items2: [{
        id: String,
        pos: Number,
        dur: Number,
        startTime: Number,
        endTime: Number,
        duration: Number
        items3: [{
                    id: String,
                    itemtype: String,
                    style: String,
                    items4: [{
                            id: String,
                            position: Number,
                            dome: String
                            }]
                    }]

        }]
   }
}

So how can I best build the Collection2 collection with the above nested schema and the best way to perform insert, update and remove queries on it.

Update:

So now as suggested by Andrei Karpushonak this is what I have got:

Item4 = new SimpleSchema({
    position: {
        type: Number
    },
    dome: {
        type: String
    }
});
    Item3 = new SimpleSchema({
    itemtype: {
        type: String
    },
    style: {
        type: String
    },
    items4: {
        type: [Item4]
    }
});

Item2 = new SimpleSchema({
    pos: {
        type: Number
    },
    dur: {
        type: Number
    },
    startTime: {
        type: Number
    },
    endTime: {
        type: Number
    },
    duration: {
        type: Number
    },
    items3 : {
        type: [Item3]
    }
});


Items = new Meteor.Collection2('items', {
    schema : new SimpleSchema({
        title: {
            type: String
        },
        Desc: {
            type: String
        },
        createdAt: {
            type: new Date()
        },
        creator: {
            type: String
        },
        invitedUsers: {
            type: [String]
        },
        items2: {
            type: [Item2]
        }
    })
});

So now I am trying to figure out how can I do the insert, update, remove operations on such a schema? Do I do for individual schemas for as whole? An example will be very helpful.

Any help will be highly appreciated.

Thanks in Advance,

Praney

Upvotes: 1

Views: 3226

Answers (1)

Andrei Karpuszonak
Andrei Karpuszonak

Reputation: 9034

You have two options:

Create sub-schema:

item2 = new SimpleSchema({
  id: String,
  pos: Number
})

item1 = new SimpleSchema({
  id: Meteor.ObjectID,
  title: String,
  items2: [item2]
});

Use dot notation:

item1 = new SimpleSchema({
  id: String,
  pos: Number,
  "item2.id": String,
  "item2.pos": String
});

I think first approach fits your model better, as you have array of objects as value of items2

Upvotes: 5

Related Questions