mongodb - How can I merge many documents into a single document and move to another collection? -


i have following situation:

  • 50 documents in 1 collection
  • want move 1 document
day: {data:data,data:data} day: {data:data,data:data} 

this data should become:

week:[day,day,day..] 

this 1 time job. want in command line or robomongo.

since can't see exact document structure, i'll try give simple solution should work no matter structure documents have.

basically want iterate on each of documents in source collection , generate array of values.

week_array = [] db.source_col.find({}).foreach( function( doc ){   week_array.push( doc ); } ); 

you have array containing documents source_collection. have construct new document , insert array @ correct attribute:

var week_doc = { "week": week_array }; 

now insert target collection:

db.target_col.insert( week_doc ); 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -