mongodb - How to script a schema change for one of my Mongo collections within Meteor -


right now, structure of programs collection is:

{    name : 'jane doe',   year : 'c16',   campyear: 'ssipc16',   roomwk1 :'21a',    roomwk2: '22a',   roomwk3: '33b',   roomwk4: '33b',   roomwk5: '25b'   week1: '1'   ... } 

i want change to:

{   name: 'jan doe',   year: {            'c16': {                    roomwk1: '21a',                    roomwk2: '22a',                    roomwk3: '33b',                    roomwk4: '33b',                    roomwk5: '25b'                   }         }    campyear: 'ssipc16',    ... }  

the change nest roomwk1 - roomwk5 under year field. how this? should done within mongo shell in meteor?

if there aren't lot of program documents, can this:

import { meteor } 'meteor/meteor'; import { mongo } 'meteor/mongo';  const programs = new mongo.collection('programs');  meteor.startup(() => {   const programs = programs.find().fetch();    (let program of programs) {     const { year, roomwk1, roomwk2, roomwk3, roomwk4, roomwk5 } = program;     program.year = { [year]: { roomwk1, roomwk2, roomwk3, roomwk4, roomwk5 } };     delete program.roomwk1;     delete program.roomwk2;     delete program.roomwk3;     delete program.roomwk4;     delete program.roomwk5;      programs.update(program._id, program);   }  }); 

it's little crude because loads each program memory , replaces in db, should work in cases collection size doesn't exceed few thousand documents.

for more sophisticated techniques, i'd recommend reading migrations section of guide.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -