Le garcon
Le garcon

Reputation: 8367

Modify collection value in Meteor

I want to write a query that would change the type of a project field from string to object.

So, if project field has the value abcd now, I want it to have an object like this: {id: 'abcd'}

So:

project: 'abcd'

Turns to:

project: {id: 'abcd'}

I have no problems doing it in mongo:

db.hello.find({}).forEach((project) => {
 project.project = {
     id: x.project
 }
 db.hello.save(x)
})

But I don't know how to do it in Meteor. So far I have:

Projects.update($set: { client: ??? } }, { multi: true });

My 2 main problems are:

  1. I don't know how to get the current value of client

  2. I don't know how to change type

Upvotes: 0

Views: 35

Answers (1)

asosnovsky
asosnovsky

Reputation: 2235

First of all, if you already ran the query, then you are aware that the db has already been adjusted yes? Because if you did run that, it would have updated all of the documents in that collection!

Please note that this should be ran server-side, I don't think that the $type is supported by all versions of minimongo.

// grab the cursor all string typed `project` fields
const cursor = Projects.find({ project: { $type : "string" } });
// grab the data from the cursor
const projects = cursor.fetch();
// Loop on each project and update
projects.forEach( project => Projects.update(project._id, {
    $set: {
        project: { id: project }
    }
}) )

Upvotes: 1

Related Questions