1

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" );
Community
  • 1
  • 1
Monnster
  • 625
  • 7
  • 16
  • What does Play! have to do with accessing MongoDB? Are you using a plugin or something? – vptheron Apr 28 '14 at 15:08
  • You code is scala code. To access MongoDB with scala, you can use http://reactivemongo.org/ Example with Play Framework [here](https://github.com/ReactiveMongo/Play-ReactiveMongo). For Id Generation, please read [this thread](https://groups.google.com/forum/#!topic/play-framework/6vth87vERwU) – yesnault Apr 29 '14 at 06:46

1 Answers1

0

hate to answer this but might help others!

-- from my research, mongodb generated ID is being done via client side, so no need to query this to sever side

Solution #1 Assign value to your ID

EX: val cID = new Object()

then include this in your save code

Solution #2 Let mongodb assign ID

EX: val comment = Comment(userId = user.id, name=user.getNameString, text = text)

Then this will return all the info together with the generated ID. You can access the ID via comment.id (in my case).

Hope this will help others

Monnster
  • 625
  • 7
  • 16