meteor-collection2 icon indicating copy to clipboard operation
meteor-collection2 copied to clipboard

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

Open nosizejosh opened this issue 7 years ago • 3 comments

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 avatar Apr 23 '18 15:04 nosizejosh

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 avatar Apr 23 '18 15:04 nosizejosh

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

nosizejosh avatar Apr 23 '18 16:04 nosizejosh

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

aldeed avatar May 23 '18 12:05 aldeed