       callWhenEnded()
 
 
-  (callWhenEnded) ->
-    console.log "> Upserting AidData commitments in MongoDB"
-
-    reader = linereader.open AIDDATA_TEMP_FILE
-
-    nextRow = ->
-      if reader.hasNextLine()
-        JSON.parse reader.nextLine()
-      else
-        null
-
-
-    # update synchronously
-    upsert = do -> 
-      upsertCount = 0
-      (row) ->
-        unless row?
-          callWhenEnded()
-          console.log "Upserted #{upsertCount} documents"
+
+
+
+
+
+  (callWhenEnded) ->
+    console.log "> Upserting locations in MongoDB"
+
+    importCollectionToMongo  "locations", LOCATIONS_TEMP_FILE, ["code"], callWhenEnded
+
+
+
+  (callWhenEnded) ->
+    console.log "> Upserting commitments in MongoDB"
+
+    importCollectionToMongo  "aiddata", AIDDATA_TEMP_FILE, ["aiddata_id"], callWhenEnded
+
+    ###
+    if USE_MONGOIMPORT   # faster
+
+      os.run "/usr/bin/mongoimport " +
+                  "-d aiddata -c aiddata --upsert --upsertFields aiddata_id " +
+                  "-u #{dbconf.mongodb.user} -p #{dbconf.mongodb.password} "+ 
+                  "#{file}",
+        (err) ->
+          unless err?
+            fs.unlink(AIDDATA_TEMP_FILE)
+
+          callWhenEnded(err)
+
+    else
+
+      import 
+
+      mongodb.collection 'aiddata', (err, coll) =>
+        if err? then callWhenEnded(err)
         else
-          aiddataColl.update(
-            { aiddata_id:row.aiddata_id }, row, { safe:true, upsert:true }, 
-            (err, docs) ->
-              if err?
+
+          reader = linereader.open AIDDATA_TEMP_FILE
+
+          nextRow = ->
+            if reader.hasNextLine()
+              JSON.parse reader.nextLine()
+            else
+              null
+
+
+          # update synchronously
+          upsert = do -> 
+            upsertCount = 0
+            (row) ->
+              unless row?
+                callWhenEnded()
                 console.log "Upserted #{upsertCount} documents"
-                console.warn "Problem upserting aiddata_id: #{row.aiddata_id}: #{err}"
-                callWhenEnded(err)
               else
-                upsertCount++
-                upsert nextRow()   # proceed to next row              
-            , true
-          )
-
-
-    upsert nextRow()
+                coll.update(
+                  { aiddata_id:row.aiddata_id }, row, { safe:true, upsert:true }, 
+                  (err, docs) ->
+                    if err?
+                      console.log "Upserted #{upsertCount} documents"
+                      console.warn "Problem upserting aiddata_id: #{row.aiddata_id}: #{err}"
+                      callWhenEnded(err)
+                    else
+                      upsertCount++
+                      if ((upsertCount % 10000) is 0) then console.log "Upsert count: #{upsertCount}" 
+                      upsert nextRow()   # proceed to next row              
+                  , true
+                )
+          upsert nextRow()
+    ###
+
+
+
+
 
 
 ], (err, results) ->

    pgclient.connect()
    callWhenEnded()

  (callWhenEnded) ->
    console.log "> Ensure aiddata_id index"
  
    aiddataColl.ensureIndex { aiddata_id : 1 }, (err) -> 
      if err? then callWhenEnded(err)
      else 
        console.log "Index for aiddata_id ensured"
        callWhenEnded()



  (callWhenEnded) ->
    console.log "> Export AidData commitments from PostgreSQL to a temporary file"

    os.mkdir TEMP_DIR
    fd = fs.openSync(AIDDATA_TEMP_FILE, 'w')

          unless v?
            delete row[k]


      row.origin = locationNameToCode[row.donor]
      row.dest = locationNameToCode[row.recipient]

      delete row.donor
      delete row.donorcode
      delete row.recipient
      delete row.recipientcode

      # assuming that stringify produces a one-liner
      fs.writeSync fd, JSON.stringify(row) + "\n"

