Advertisement
zinch

Update MongoDB collection

Jun 15th, 2015
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 0.48 KB | None | 0 0
  1. import com.mongodb.MongoClient
  2. import org.bson.Document as doc
  3.  
  4. def mongoClient = new MongoClient()
  5. def database = mongoClient.getDatabase('school')
  6. def collection = database.getCollection('students')
  7.  
  8. collection.find()
  9.     .projection(['scores':1] as doc)
  10.     .each {
  11.         def minScore = it.scores.findAll { it.type == 'homework'}.min { it.score }
  12.         it.scores.remove(minScore)
  13.         collection.updateOne(['_id':it._id] as doc, ['$set':['scores':it.scores]] as doc)
  14.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement