Unable to insert with subdocument array


#1

I have a SimpleSchema as follows (sorry folks, it’s in Coffeescript but you’ll be able to get the gist):

MyObjectSchema = new SimpleSchema
    'parentId':
        type: SimpleSchema.RegEx.Id
    'elements':
        type: [Object]
        maxCount: 2
    'elements.$.id'
        type: SimpleSchema.RegEx.Id
    'elements.$.number'
        type: Number
        min: 1
        max: 2

//Attach the schema
MyObjects.attachSchema MyObjectSchema

Trying to insert the first record in a server method as follows:

myParentId = //some ID I set
userId = Meteor.userId()
MyObjects.insert { parentId: myParentId, elements: [ { id: userId, number: 1 } ] }

doesn’t work. I can run the same on the mongo side. Here’s the server console error:

SimpleSchema.clean: filtered out value that would have affected key "elements.$.id", which is not allowed by the schema
SimpleSchema.clean: filtered out value that would have affected key "elements.$.number", which is not allowed by the schema```

I feel I'm not declaring the schema correctly or that something about the write doesn't work. I basically need an array of elements on MyObject, I need to add the first one upon creation (insert), and the 2nd one later.

Also, I am wondering about performance with having elements be an array of Arrays or an array of Objects. Any insight on this would be great too! Thanks, everyone!