question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

2reactions
aldeedcommented, Apr 24, 2018

@nosizejosh A few issues:

  1. blackbox is for objects, not arrays. But you shouldn’t need it.
  2. Your schema is missing "tenants.$": Object (meaning, each array item must be an object)
  3. In your $push, 'tenants.id' should be just id and 'tenants.name' should be just name. You’re pushing that whole object into tenants array, so those keys are already scoped.
0reactions
nosizejoshcommented, Apr 25, 2018

@aldeed thank you so much for you reply, it works. I actually ended-up leaving the array of objects to use a simple array to store only tenant ids as my use case is similar to @alannings in this thread. Question: can an array of objects be indexed as well?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Help with inserting new objects into array of objects ... - GitHub
I have been struggling to insert objects into an array on the Meteor user ... into array of objects through update statement with...
Read more >
How to add and update objects into array of objects?
Try to add the object to its specific property workExperience . You can also update the property.
Read more >
JavaScript Array of Objects Tutorial – How to Create, Update ...
Add a new object at the end - Array.push​​ To add an object at the last position, use Array. push .
Read more >
Array.prototype.push() - JavaScript - MDN Web Docs
The push() method adds one or more elements to the end of an array and returns the new length of the array.
Read more >
How to push an array into the object in JavaScript
An array can be inserted into the object with push() function, below examples illustrate the above approach: Example 1: Javascript. Javascript ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found