How can I get the last inserted ID? I need to get latest comments id. Please see below codes:
Snoop.findOneById(snoopId) match {
case Some(snoop) => {
user.groups.find(ug => ug.groupId == snoop.groupId).map { ug =>
val prop = Property.findOneById(snoop.propertyId).get
val comment = Comment(userId = user.id, name=user.getNameString, text = text)
val snoopSave = snoop.copy(comments = comment :: snoop.comments)
val sComment = Snoop.save(snoopSave)
}
.getOrElse(None)
}
case _ => None
}
Mongodb structure :
{
"_id": ObjectId("532725c3ff5d00682f5a0b9b"),
"_t": "models.Snoop",
"propertyId": ObjectId("532725bfff5d00682f5a0b99"),
"groupId": ObjectId("529f6461e4b008b565b4358c"),
"userId": ObjectId("532717e5ff5d00682f5a0b83"),
"price": 995000,
"monthlyFees": 0,
"comments": {
"0": {
"_t": "models.Comment",
"_id": ObjectId("534bce0b7d374bf1b5a170ba"),
"userId": ObjectId("529f6461e4b008b565b4358b"),
"name": "monmon",
"text": "test",
"createDate": ISODate("2014-04-14T12: 01: 15.627Z"),
"replies": {
"0": {
"_t": "models.Comment",
"_id": ObjectId("534bd1b67d374bf1b5a170be"),
"userId": ObjectId("529f6461e4b008b565b4358b"),
"name": "monmon",
"text": "monnmonn",
"createDate": ISODate("2014-04-14T12: 16: 54.752Z"),
"replies": [
]
},
"1": {
"_t": "models.Comment",
"_id": ObjectId("534bcf437d374bf1b5a170bc"),
"userId": ObjectId("529f6461e4b008b565b4358b"),
"name": "monmon",
"text": "test test test",
"createDate": ISODate("2014-04-14T12: 06: 27.896Z"),
"replies": [
]
}
}
}
}
Also, I tried Get ID of last inserted document in a mongoDB w/ Java driver but did not work :
val sComment = Snoop.save(snoopSave)
ObjectId id = (ObjectId)sComment.get( "_id" );