简体   繁体   中英

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

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 }
    }
}) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM