Why does JSONFileToSave uses UUID as default key for a document instead of ObjectId?

27 views
Skip to first unread message

Alexey Sadovoy

unread,
May 6, 2016, 4:21:01 AM5/6/16
to ReactiveMongo - http://reactivemongo.org
Hello. I just want to ask why JSONFileToSave class which is declared as


// Actually this is a helper object, not class, but this is not important
object JSONFileToSave {
 
def apply[N](filename: N,
 contentType
: Option[String] = None,
 uploadDate
: Option[Long] = None,
 metadata
: JsObject = Json.obj(),
 id
: JsValue = Json.toJson(UUID.randomUUID().toString)) // <- This is what is mean
   
(implicit naming: DefaultFileToSave.FileName[N]): JSONFileToSave = new JSONFileToSave(naming(filename), contentType, uploadDate, metadata, id)
}
Well, why it uses UUID as a default key instead of ObjectId? I've replaced it with newly generated ObjectId in my code, but I'm new to MongoDB and curious about such kind of decision


def save = SecuredAction.async(gridFSBodyParser(gridFS, (filename, contentType) =>
 
JSONFileToSave(id = Json.toJson(BSONObjectID.generate)(BSONObjectIDFormat), // <- Replace default implementation
 filename
= Some(filename), contentType = contentType))) { request =>
 
 request
.body.files.headOption.map(_.ref.mapTo[JSONReadFile].map { meta =>
 log
.debug(s"PDF $meta is saved successfully")
 
Created[JsValue](SuccessResponse.build(request, Json.toJson(meta)(JSONReadFileWriter.writes)))
 
}.recover {
 
// Handling and logging
 
}
}


I thought that default key should be ObjectId. At least it is preferred. Can you please explain why it is done is that way? Thanks

P.S. Play 2.3, ReactiveMongo 0.11

Alexey Sadovoy

unread,
May 6, 2016, 5:20:34 AM5/6/16
to ReactiveMongo - http://reactivemongo.org
Sorry for the grammar. It was written in rush

cchantep

unread,
May 7, 2016, 8:06:04 AM5/7/16
to ReactiveMongo - http://reactivemongo.org
Because there is no ObjectId type in JSON.

Alexey Sadovoy

unread,
May 10, 2016, 6:43:15 AM5/10/16
to ReactiveMongo - http://reactivemongo.org
Under ObjectId I mean hexadeciamal identifier like "507f191e810c19729de860ea"

суббота, 7 мая 2016 г., 15:06:04 UTC+3 пользователь cchantep написал:

Cédric Chantepie

unread,
May 11, 2016, 3:14:21 AM5/11/16
to ReactiveMongo - http://reactivemongo.org
The only requirement for GridFS is to provide unique ID, so there is no reason not to use UUID String which available in any JVM.
Reply all
Reply to author
Forward
0 new messages