Meteor-Community-Packages / meteor-collection2

A Meteor package that extends Mongo.Collection to provide support for specifying a schema and then validating against that schema when inserting and updating.
https://packosphere.com/aldeed/collection2
MIT License
1.02k stars 108 forks source link

Help with inserting new objects into array of objects through update statement with $push #379

Open nosizejosh opened 6 years ago

nosizejosh commented 6 years ago

I have been struggling to insert objects into an array on the Meteor user document and keep getting errors with my query. I am trying to achieve similar structure as how emails are stored using the accounts package.

ie image

below is my schema

tenants: {
type: Array, //Object optional: true, blackbox: true }, "tenants.$.id": { // the organisation is type: String, }, "tenants.$.name": { // the organisation is type: String, },

and my update query as below

> _id: Meteor.userId(),
>                     modifier: {
>                       $set:{currentType: "special", currentTenant: "tenantId"}, 
>                       $push: { 
>                         'tenants': {                     
>                             'tenants.id':"tenantId",
>                             'tenants.name': "test tenantname"
>                         }
>                        }
>                       }

have tried so many different version and keep getting so many errors. This current version give no errors but doesn't insert either. the push part of the query just fails or does nothing.

Im using collection2, simplscema etc

Please help @aldeed

nosizejosh commented 6 years ago

changed schema to below but still no errors and no inserts either.

tenants: {  // all organaisations a user has access to
    type: Array, //Object,
    optional: true,
    blackbox: true
},

"tenants.$": { // all organaisations a user has access to type: Object }, "tenants.$.id": { // the organisation is type: String, }, "tenants.$.name": { // the organisation is type: String, },

nosizejosh commented 6 years ago

commenting out black box true results in the error below tenants.0.tenants.id is not allowed by the schema

aldeed commented 6 years ago

Arrays cannot be blackbox, only objects. Try putting blackbox: true on tenants.$ instead of on tenants.