/* This is a JavaScript script for mongodb. It constructs a new version of the Artists collection of the moma database from the one created by mongoimport and the Artists.json files downloaded from the Museum of Modern Art: https://github.com/MuseumofModernArt/collection It makes the following changes: The auto-generated _id field is replaced by the ConstituentID in Artists For each artist document, we add an array field which is an array of the ObjectIDs of the artist's artworks */ conn = new Mongo("mongodb.cs.oberlin.edu") db1 = conn.getDB("moma1") db2 = conn.getDB("moma2") db2.Artists.drop() db2.createCollection("Artists") cursor1 = db1.Artists.find() docs = [] n = 0 while(cursor1.hasNext()){ doc = cursor1.next() doc._id = doc.ConstituentID cursor2 = db1.Artworks.find({ "Artist.ConstituentID" : doc.ConstituentID }) doc.Artworks = [] while(cursor2.hasNext()){ doc.Artworks.push(cursor2.next().ObjectID) } docs.push(doc) n++ if(n%1000==0){ print("processed "+n+" artists") } } print("inserting "+n+" documents into Artists collection") db2.Artists.insertMany(docs)