简体   繁体   English

修改流星中的收集值

[英]Modify collection value in Meteor

I want to write a query that would change the type of a project field from string to object . 我想编写一个查询,该查询会将project field的类型从string更改为object

So, if project field has the value abcd now, I want it to have an object like this: {id: 'abcd'} 因此,如果project字段现在的值为abcd ,我希望它具有这样的对象: {id: 'abcd'}

So: 所以:

project: 'abcd'

Turns to: 变成:

project: {id: 'abcd'}

I have no problems doing it in mongo: 我在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. 请注意,这应该在服务器端运行,我认为minitype的所有版本都不支持$type

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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