2

我在这里有一个 mongodb 数据库中的两个文档的集合(我正在使用 robomongo)我收藏的文件列表

我从 如何将多个文档合并到一个文档中并移动到另一个集合中获取了解决方案?

但是我最终是空的

错误

原始文件的代码如下:

{
"_id" : ObjectId("5534e79726092822df7528ff"),
"labels" : {
    "urls" : "http://planet-e.nethttp://planetecommunications.bandcamp.comhttp://www.discogs.com/user/planetedetroithttp://www.facebook.com/planetedetroithttp://www.flickr.com/photos/planetedetroithttp://plus.google.com/100841702106447505236http://myspace.com/planetecomhttp://myspace.com/planetedetroithttp://soundcloud.com/planetedetroithttp://twitter.com/planetedetroithttp://vimeo.com/user1265384http://www.youtube.com/user/planetedetroithttp://en.wikipedia.org/wiki/Planet_E_Communications",
    "id" : "1",
    "name" : "Planet E",
    "sublabels" : "Antidote (4)Community ProjectsGuilty PleasuresI Ner Zon SoundsPlanet E Communications, Inc.TWPENTY",
    "contactinfo" : "Planet E Communications\nP.O. Box 27218\nDetroit, 48227, USA\n\np: 313.874.8729 \nf: 313.874.8732\n\nemail: info AT Planet-e DOT net\n",
    "profile" : "Classic Techno label from Detroit, USA.\n[b]Label owner:[/b] [a=Carl Craig].\n",
    "dataquality" : "Needs Vote"
}
}


 {
"_id" : ObjectId("5534e79726092822df752900"),
"labels" : {
    "id" : "2",
    "name" : "Earthtones Recordings",
    "sublabels" : [],
    "contactinfo" : "Seasons Recordings\n2236 Pacific Avenue\nSuite D\nCosta Mesa, CA  92627\n\ntel: +1.949.574.5255\nfax: +1.949.574.0255\n\nemail: <a href=\"mailto:jthinnes@seasonsrecordings.com\">jthinnes@seasonsrecordings.com</a>\n",
    "profile" : "California deep house label founded by Jamie Thinnes. Now defunct and continued as [l=Seasons Recordings].",
    "dataquality" : "Correct",
    "urls" : "http://www.seasonsrecordings.com/"
}
}

我想将两个文档合并为一个标题“标签”下的文档

4

1 回答 1

0

试试下面的代码片段:

var label_array = [];
db.labels.find().forEach(function (doc){
    label_array.push(doc.labels);
});
db.label.insert({"label": label_array});
db.label.find();

结果

/* 1 */
{
    "_id" : ObjectId("55353a2d6056a9466b1a3a9c"),
    "label" : [ 
        {
            "urls" : "http://planet-e.nethttp://planetecommunications.bandcamp.comhttp://www.discogs.com/user/planetedetroithttp://www.facebook.com/planetedetroithttp://www.flickr.com/photos/planetedetroithttp://plus.google.com/100841702106447505236http://myspace.com/planetecomhttp://myspace.com/planetedetroithttp://soundcloud.com/planetedetroithttp://twitter.com/planetedetroithttp://vimeo.com/user1265384http://www.youtube.com/user/planetedetroithttp://en.wikipedia.org/wiki/Planet_E_Communications",
            "id" : "1",
            "name" : "Planet E",
            "sublabels" : "Antidote (4)Community ProjectsGuilty PleasuresI Ner Zon SoundsPlanet E Communications, Inc.TWPENTY",
            "contactinfo" : "Planet E Communications\nP.O. Box 27218\nDetroit, 48227, USA\n\np: 313.874.8729 \nf: 313.874.8732\n\nemail: info AT Planet-e DOT net\n",
            "profile" : "Classic Techno label from Detroit, USA.\n[b]Label owner:[/b] [a=Carl Craig].\n",
            "dataquality" : "Needs Vote"
        }, 
        {
            "id" : "2",
            "name" : "Earthtones Recordings",
            "sublabels" : [],
            "contactinfo" : "Seasons Recordings\n2236 Pacific Avenue\nSuite D\nCosta Mesa, CA  92627\n\ntel: +1.949.574.5255\nfax: +1.949.574.0255\n\nemail: <a href=\"mailto:jthinnes@seasonsrecordings.com\">jthinnes@seasonsrecordings.com</a>\n",
            "profile" : "California deep house label founded by Jamie Thinnes. Now defunct and continued as [l=Seasons Recordings].",
            "dataquality" : "Correct",
            "urls" : "http://www.seasonsrecordings.com/"
        }
    ]
}
于 2015-04-20T17:42:41.767 回答