0

I am trying to push arrays from one collection to another. this is the code I used in my server js

    updateSettings: function(generalValue) {
     let userId = Meteor.userId();
       let settingsDetails = GeneralSettings.findOne({
   "_id": generalValue
 });
            Meteor.users.update({
     _id: userId
   }, {
     $push: {

       "settings._id": generalValue,
       "settings.name": settingsDetails.name,
       "settings.description": settingsDetails.description,
       "settings.tag": settingsDetails.tag,
        "settings.type": settingsDetails.type,
       "settings.status": settingsDetails.status
     }


   })

}

updateSettings is the meteor method. GeneralSettings is the first collection and user is the second collection. I want to push arrays from GeneralSettings to users collection. While I try this the result i got is like

 "settings" : {
    "_id" : [ 
        "GdfaHPoT5FXW78aw5", 
        "GdfaHPoT5FXW78awi"

    ],
    "name" : [ 
        "URL", 
        "TEXT" 

    ],
    "description" : [ 
        "https://docs.mongodb.org/manual/reference/method/db.collection.update/", 
        "this is a random text" 

    ],
    "tag" : [ 
        "This is a demo of an Url selected", 
        "demo for random text"
    ],
    "type" : [ 
        "url", 
        "text"
    ],
    "status" : [ 
        false, 
        false
    ]
}

But the output I want is

 "settings" : {
    "_id" : "GdfaHPoT5FXW78aw5",

    "name" :  "URL", 

    "description" :  
        "https://docs.mongodb.org/manual/reference/method/db.collection.update/", 

    "tag" :"This is a demo of an Url selected", 

    "type" :  "url",


    "status" :    false 



},

What changes to be made in my server.js inorder to get this output

1
  • Try using $set instead of $push here. You push to arrays, but I don't think settings._id, for example, is an array Commented Apr 18, 2016 at 11:50

1 Answer 1

3

This is one case where you "do not want" to use "dot notation". The $push operator expects and Object or is basically going to add the "right side" to the array named in the "left side key":

// Make your life easy and just update this object
settingDetails._id = generalValue;

// Then you are just "pushing" the whole thing into the "settings" array
Meteor.users.update(
  { _id: userId }, 
  { "$push": { "settings": settingDetails } }
)

When you used "dot notation" on each item, then that is asking to create "arrays" for "each" of the individual "keys" provided. So it's just "one" array key, and the object to add as the argument.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.