mongodb - Get newly inserted document BSONObjectID -
i need return newly inserted object. read can generate own id that's not case.
def create(repo: string) = action.async(parse.json) { implicit req => val id = bsonobjectid.generate collection(repo).insert($("_id " -> id)).map { last => if(last.ok) ok(json.tojson($("_id"->id)++$("success"->true))) else badrequest($("success"->false)) } }
and i'm generating records this:
{ "_id": { "$oid": "556dfb2021c76e3b1c566915" }, "_id ": { "$oid": "556dfb1f5f00006100d4a0bc" } }
you should not generate mongodb _id
manually unless know sure unique or have deal duplicated key exception
in code.
if insert new document without _id
, mongodb generate 1 during insert process. if want _id
back, can request mongodb document newly created _id
.
normally drivers handle part automatically you.
Comments
Post a Comment