Help with inserting new objects into array of objects through update statement with $push
See original GitHub issueI 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
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:
- Created 5 years ago
- Reactions:1
- Comments:6
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@nosizejosh A few issues:
blackbox
is for objects, not arrays. But you shouldn’t need it."tenants.$": Object
(meaning, each array item must be an object)'tenants.id'
should be justid
and'tenants.name'
should be justname
. You’re pushing that whole object intotenants
array, so those keys are already scoped.@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?