在mongodb中合并多个文档

时间:2015-04-20 17:01:14

标签: mongodb robo3t

我这里有一个mongodb数据库中的两个文档的集合(我正在使用robomongo)list of documents in my collection

我从中获得了解决方案 How can I merge many documents into a single document and move to another collection?

但是我结束了空

error

原始文件的代码如下:

{
"_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/"
}
}

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

1 个答案:

答案 0 :(得分: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/"
        }
    ]
}