Insert and Fetch the Inserted Document

15 views
Skip to first unread message

Joe San

unread,
Oct 24, 2015, 2:55:50 AM10/24/15
to ReactiveMongo - http://reactivemongo.org
How do I go about doing this with just a single query? What I need is to fetch the newly inserted record so that I have access to its ObjectId

What I have currently is the following:

userCollection.insert(encryptedUser).map { _ =>
Right(user)
}.recover {
case l: WriteResult =>
if (l.code contains 11000)
Left(ServiceError("Email already exists"))
else
Left(ServiceError(l.errmsg.getOrElse("failure!")))
}

The user reference that I return in the Right type is the incoming request to this method and it does not contain the ObjectId as this ObjectId would be generated by the MongoDB system.
Reply all
Reply to author
Forward
0 new messages