[英]merge two fields in document using mongodb
Below is my JSON, is there any way where I can merge multiple fields of a same document. 下面是我的JSON,有什么方法可以合并同一文档的多个字段。
"_id" : 1,
"sem" : 1,
"achieve" : [ 70, 87, 90 ]
I am expecting the output as 我期望输出为
"_id" : 1,
"sem" : [1,70, 87, 90],
"achieve" : [ 70, 87, 90 ]
One of the simple methods just to achieve what you are asking. 满足您要求的简单方法之一。 You can create a loop for all the documents of the collection though.
但是,您可以为集合的所有文档创建一个循环。 The code below has 'stack' collection having just one document, the one you mentioned.
下面的代码具有“堆栈”集合,其中仅包含一个您提到的文档。
doc = db.stack.findOne();
var tmp = doc.sem;
doc.sem = new Array();
doc.sem.push(tmp);
i = 0;
for(i = 0;i<doc.achieve.length;i++){
doc.sem.push(doc.achieve[i]);
}
db.stack.update({sem:1},doc);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.