[英]Update a field to be further nested in a document - MongoDB/Node
I am writing a migration with migrate-mongo and the Node driver for Mongo which both have not-so-great documentation.我正在使用migrate-mongo和Mongo 的 Node 驱动程序编写迁移,它们都没有那么好的文档。 I am trying to take a field and nest it one level further.
我正在尝试获取一个字段并将其进一步嵌套。 Here is an example of the original structure:
这是原始结构的示例:
{
"_id": {"$oid":"xxxxxxxx"},
"module": "lalala",
"settings": {
"yAxis": {
"title": {
"text": "TITLE"
}
}
}
I would like to take the yAxis field and its contents and nest it under a "highcharts" field so it ends up like this:我想获取 yAxis 字段及其内容并将其嵌套在“highcharts”字段下,因此它最终如下所示:
"settings": {
"highcharts": {
"yAxis": {
"title": {
"text": "TITLE"
}
}
}
}
I saw this Update field with value from a nested field and was hoping I could use that $ operator to just take the yAxis contents and stick them back in but that isn't working.我看到这个Update 字段具有来自嵌套字段的值,并希望我可以使用 $ 运算符来获取 yAxis 内容并将它们放回原处,但这是行不通的。 (The yAxis field now just reads the string literal '$settings.yAxis')
(yAxis 字段现在只读取字符串文字“$settings.yAxis”)
async up(db) {
const panels = db.collection('panels');
await panels.updateMany({module: 'lalala'},
{$set: {'settings.highcharts.yAxis': '$settings.yAxis'}});
await panels.updateMany({module: 'lalala'},
{$unset: {'settings.yAxis': ''}});
I also thought maybe I should iterate through the documents, parse them as JSON and grab the contents of yAxis, and insert that into a new 'highcharts' field, but that's using await in a forEach which doesn't work.我还想也许我应该遍历文档,将它们解析为 JSON 并获取 yAxis 的内容,然后将其插入到新的“highcharts”字段中,但这是在 forEach 中使用 await,这是行不通的。
I ideally am doing this async so that I can do multiple operations in a single migration.理想情况下,我正在执行此异步操作,以便我可以在一次迁移中执行多项操作。 Otherwise I would have to set the new 'highcharts' field in one migration and unset the old 'yAxis' in a different migration which could lead to problems if one fails but the other doesn't.
否则,我将不得不在一个迁移中设置新的“highcharts”字段,并在另一个迁移中取消设置旧的“yAxis”,如果一个失败而另一个失败,这可能会导致问题。
Somewhat stuck here, anything helps.有点卡在这里,任何帮助。 Thanks!
谢谢!
OK I was close and I dont understand why, but putting brackets around my $set was what I needed.好吧,我已经很接近了,但我不明白为什么,但是我需要用括号括起我的 $set 。
await panels.updateMany({module: 'lalala'},
[ {$set: {'settings.highcharts.yAxis': '$settings.yAxis'}} ] );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.