我在mongodb中有一个集合,其中字段嵌套在语言根下:
{
en: {
title: "eng title",
content: "eng content",
},
it: {
title: "it title",
content: "it content"
}
//common attributes for all languages
images: {
mainImage: "dataURL",
thumbImage: "dataURL"
}
}
我有一个名为'currentLang'的变量;我需要通过标题查找文档,仅选择"currentLang"对象和常见字段(例如此示例中的图像); 但对于"currentLang"对象,我希望输出的文档不是嵌套的;例如,当currentLang = "en"时。
期望输出:
{
title: "eng title",
content: "eng content",
images: {
mainImage: "dataURL",
thumbImage: "dataURL"
}
}
这是可能的吗?
project.title =
而不是project["title"] =
(您可以使用点符号表示法即时创建对象内的新字段 - 只要它不是嵌套未定义即可)。 - dylanh724