Skip to content

Instantly share code, notes, and snippets.

@Yona-Appletree
Created December 15, 2017 22:56
Show Gist options
  • Save Yona-Appletree/29be816ca74a0d93cdf9e6f5e23dda15 to your computer and use it in GitHub Desktop.
Save Yona-Appletree/29be816ca74a0d93cdf9e6f5e23dda15 to your computer and use it in GitHub Desktop.
/**
* Helper for building strings using mongo update operators. By declaring all the operators as variables in this object,
* they can be used without special escape. e.g. MongoUpdateString { """{$set:{"name":"Luke Skywalker"}}""" }
*/
object MongoUpdateString {
inline operator fun invoke(callback: MongoUpdateString.() -> String) = callback()
val currentDate = "\$currentDate"
val inc = "\$inc"
val min = "\$min"
val max = "\$max"
val mul = "\$mul"
val rename = "\$rename"
val set = "\$set"
val setOnInsert = "\$setOnInsert"
val unset = "\$unset"
val addToSet = "\$addToSet"
val pop = "\$pop"
val pull = "\$pull"
val push = "\$push"
val pullAll = "\$pullAll"
val each = "\$each"
val position = "\$position"
val slice = "\$slice"
val sort = "\$sort"
val bit = "\$bit"
}
/**
* Helper for building strings using mongo query operators. By declaring all the operators as variables in this object,
* they can be used without special escape. e.g. MongoQueryString { """{"age":{$gt:21}}""" }
*/
object MongoQueryString {
inline operator fun invoke(callback: MongoQueryString.() -> String) = callback()
val eq = "\$eq"
val gt = "\$gt"
val gte = "\$gte"
val In = "\$in"
val lt = "\$lt"
val lte = "\$lte"
val ne = "\$ne"
val nin = "\$nin"
val and = "\$and"
val not = "\$not"
val nor = "\$nor"
val or = "\$or"
val exists = "\$exists"
val type = "\$type"
val expr = "\$expr"
val jsonSchema = "\$jsonSchema"
val mod = "\$mod"
val regex = "\$regex"
val text = "\$text"
val where = "\$where"
val geoIntersects = "\$geoIntersects"
val geoWithin = "\$geoWithin"
val near = "\$near"
val nearSphere = "\$nearSphere"
val all = "\$all"
val size = "\$size"
val bitsAllClear = "\$bitsAllClear"
val bitsAllSet = "\$bitsAllSet"
val bitsAnyClear = "\$bitsAnyClear"
val bitsAnySet = "\$bitsAnySet"
val comment = "\$comment"
val meta = "\$meta"
val slice = "\$slice"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment