Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update meteor collection without removing or overriding existing fields

I don't know why but if i try to update an existing field using the $set method, any existing fields are replaced in the same context.

For example. Say i have an existing collection with the following fields.

Name of collection: Ticket

{profile: {name: "Test", placement: 1}, requestor: _id}

When i attempt to add/update fields to this collection like this:

 var ticket = Meteor.tickets.findOne({_id: ticketID});

 if(ticket){
    Meteor.users.update(ticket, {
                        $set: profile: {name: "Test2", new_fields: "value"}
                    });
 }

The collection gets updated and the name field changes but placement is removed and no longer there. This is also true if i remove the name field. How do we properly update a meteor collection without having to keep passing the same structure over and over?

like image 949
Warz Avatar asked Jun 04 '13 23:06

Warz


People also ask

How do I update my meteor version?

The easiest way is to run meteor --version inside your project folder. If you run this command outside of your project folder, it will show the Meteor version that is installed on your machine.

What is $Set in MongoDB?

$set outputs documents that contain all existing fields from the input documents and newly added fields. The $set stage is an alias for $addFields. Both stages are equivalent to a $project stage that explicitly specifies all existing fields in the input documents and adds the new fields.

How do I link my meteor to MongoDB?

Open a terminal window and run meteor command. It will start running on localhost:3000 if you have not changed to port. Go to Robomongo (or your favorite mongodb client software) and create a new connection, making sure to change the connection address to localhost and the given the port number.


1 Answers

Just do this:

$set: {"profile.name": "Test2", "profile.new_fields": "value"}

I.e. You were replacing the whole hash. Instead you can update the fields within the hash.

like image 67
BraveNewCurrency Avatar answered Oct 14 '22 17:10

BraveNewCurrency