2015-04-20 1 views
1

J'ai ici une collection de deux documents dans une base de données MongoDB (J'utilise robomongo) list of documents in my collectionLa fusion de plusieurs documents dans MongoDB

Je pris la solution de How can I merge many documents into a single document and move to another collection?

Cependant, je suis finir vide

error

Le code des documents originaux sont les suivantes:

{ 
"_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:[email protected]\">[email protected]</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/" 
} 
} 

Je veux combiner les documents en un seul document sous un seul titre "label"

+0

images sont grandes Si vous êtes accompagné d'un code réel, pouvez-vous s'il vous plaît modifier votre question pour inclure le schéma des documents et ce que vous attendez. – chridam

+0

Je l'ai édité. Mais le message original a déclaré que le schéma est hors de propos –

Répondre

0

Essayez le code suivant:

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

Résultat

/* 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:[email protected]\">[email protected]</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/" 
     } 
    ] 
} 
+0

Je reçois le suivant { "_id": ObjectId ("55353bba6da1f1bc6aaec7c2"), "label": [] } –

+0

@DhirajEadara Mes excuses, j'ai oublié de changer la source nom de la collection. Veuillez vérifier ma modification et réessayer. – chridam

+1

Oui, je l'ai. Merci –