我有一个小的mongo文档,看起来像这样:
{
"_id": {
"$oid": "5aa441e898cc0b32a819be0c"
},
"details": {
"Artist": "Cyndi Lauper",
"Album": "She's So Unusual",
"ReleaseYear": 1983
},
"SongID": 1,
"SongTitle": "Girls Just Want To Have Fund"
}
我需要将Artist元素从details数组中拉出来,但将数据保存在整个文档中。生成的文档需要如下所示:
{
"_id": {
"$oid": "5aa441e898cc0b32a819be0c"
},
"details": {
"Album": "She's So Unusual",
"ReleaseYear": 1983
},
"SongID": 1,
"SongTitle": "Girls Just Want To Have Fund",
"Artist": "Cyndi Lauper"
}
我是mongo的新手,所以我不能坚强。该系列被称为歌曲,所以我想我需要做这样的事情:
db.songs.updateOne({},{$set : {"Artist":$pull{"detail.Artist"}}})
非常感谢任何帮助和/或建议。
乔纳森
答案 0 :(得分:0)
您可以在MongoDB shell中编写类似的内容:
var doc={doc:{key42:"value"},somekey:"value"};
doc.key42=doc.doc.key42;
delete doc.doc.key42;
之后你可以写
doc
然后按Enter键。 输出值为:
{doc:{},somekey:"value",key42:"value"};
然后你可以保存doc:
db.collection.save(doc);