@@ -34,39 +34,65 @@ Promise.all([pluginManager.dbConnection("countly"), pluginManager.dbConnection("
3434 //PROCESS COLLECTIONS FOR EACH APP
3535 for ( let i = 0 ; i < apps . length ; i ++ ) {
3636 console . log ( "Processing app: " + apps [ i ] . name ) ;
37- var collectionName = drillCommon . getCollectionName ( "[CLY]_session" , apps [ i ] . _id + "" ) ;
38- console . log ( "Processing collection: " + collectionName ) ;
37+ var collectionName0 = drillCommon . getCollectionName ( "[CLY]_session" , apps [ i ] . _id + "" ) ;
3938
40- const cursor = drillDb . collection ( collectionName ) . find ( query_drill , { "_id" : 1 , "custom" : 1 } ) ;
41- //FOR EACH DOCUMENT
42- var updates = [ ] ;
43- while ( await cursor . hasNext ( ) ) {
44- var doc = await cursor . next ( ) ;
45- if ( doc . custom ) {
46- var updateDoc = { } ;
47- let updateMe = false ;
48- for ( var key in doc . custom ) {
49- if ( doc . custom [ key ] && typeof doc . custom [ key ] === "object" ) {
50- var specialKeys = [ "$set" , "$addToSet" , "$push" , "$pull" , "$inc" , "$min" , "$max" , "$setOnce" ] ;
51- for ( var z = 0 ; z < specialKeys . length ; z ++ ) {
52- if ( doc . custom [ key ] [ specialKeys [ z ] ] ) {
53- updateDoc [ "custom." + key ] = doc . custom [ key ] [ specialKeys [ z ] ] ;
54- updateMe = true ;
39+ var collections = [ "drill_events" , collectionName0 ] ;
40+
41+ for ( var z1 = 0 ; z1 < collections . length ; z1 ++ ) {
42+ var collectionName = collections [ z1 ] ;
43+ console . log ( "Processing collection: " + collectionName ) ;
44+
45+ if ( collectionName === "drill_events" ) {
46+ query_drill . a = apps [ i ] . _id + "" ;
47+ query_drill . e = "[CLY]_session" ;
48+ }
49+ else {
50+ delete query_drill . a ;
51+ delete query_drill . e ;
52+ }
53+
54+ const cursor = drillDb . collection ( collectionName ) . find ( query_drill , { "_id" : 1 , "custom" : 1 } ) ;
55+ //FOR EACH DOCUMENT
56+ var updates = [ ] ;
57+ while ( await cursor . hasNext ( ) ) {
58+ var doc = await cursor . next ( ) ;
59+ if ( doc . custom ) {
60+ var updateDoc = { } ;
61+ let updateMe = false ;
62+ for ( var key in doc . custom ) {
63+ if ( doc . custom [ key ] && typeof doc . custom [ key ] === "object" ) {
64+ var specialKeys = [ "$set" , "$addToSet" , "$push" , "$pull" , "$inc" , "$min" , "$max" , "$setOnce" ] ;
65+ for ( var z = 0 ; z < specialKeys . length ; z ++ ) {
66+ if ( doc . custom [ key ] [ specialKeys [ z ] ] ) {
67+ updateDoc [ "custom." + key ] = doc . custom [ key ] [ specialKeys [ z ] ] ;
68+ updateMe = true ;
69+ }
5570 }
5671 }
5772 }
73+ if ( updateMe ) {
74+ updates . push ( {
75+ 'updateOne' : {
76+ 'filter' : { '_id' : doc . _id } ,
77+ 'update' : { '$set' : updateDoc } ,
78+ 'upsert' : false
79+ }
80+ } ) ;
81+ }
5882 }
59- if ( updateMe ) {
60- updates . push ( {
61- 'updateOne' : {
62- 'filter' : { '_id' : doc . _id } ,
63- 'update' : { '$set' : updateDoc } ,
64- 'upsert' : false
65- }
66- } ) ;
83+ if ( updates . length === 500 ) {
84+ if ( dry_run ) {
85+ console . log ( "DRY RUN: Would update " + updates . length + " docs in " + collectionName ) ;
86+ console . log ( JSON . stringify ( updates ) ) ;
87+ }
88+ else {
89+ console . log ( "updating" ) ;
90+ await drillDb . collection ( collectionName ) . bulkWrite ( updates , { "ordered" : false } ) ;
91+ }
92+ updates = [ ] ;
6793 }
6894 }
69- if ( updates . length === 500 ) {
95+ if ( updates . length > 0 ) {
7096 if ( dry_run ) {
7197 console . log ( "DRY RUN: Would update " + updates . length + " docs in " + collectionName ) ;
7298 console . log ( JSON . stringify ( updates ) ) ;
@@ -78,16 +104,6 @@ Promise.all([pluginManager.dbConnection("countly"), pluginManager.dbConnection("
78104 updates = [ ] ;
79105 }
80106 }
81- if ( updates . length > 0 ) {
82- if ( dry_run ) {
83- console . log ( "DRY RUN: Would update " + updates . length + " docs in " + collectionName ) ;
84- console . log ( JSON . stringify ( updates ) ) ;
85- }
86- else {
87- await drillDb . collection ( collectionName ) . bulkWrite ( updates , { "ordered" : false } ) ;
88- }
89- updates = [ ] ;
90- }
91107 }
92108 }
93109 catch ( err ) {
0 commit comments