Q:

Get _id from MongoDB using Scala

For a given JSON how do I get the _id to use it as an id for inserting in another JSON?
Tried to get the ID as shown below but does not return correct results.
private def getModelRunId(): List[String] = {
  val resultsCursor: List[DBObject] =
    modelRunResultsCollection.find(MongoDBObject.empty, MongoDBObject(FIELD_ID -> 1)).toList
  println("resultsCursor >>>>>>>>>>>>>>>>>> " +  resultsCursor)
  resultsCursor.map(x => (Json.parse(x.toString()) \ FIELD_ID).asOpt[String]).flatten
}

{
"_id": ObjectId("5269723bd516ec3a69f3639e"),
"modelRunId": ObjectId("5269723ad516ec3a69f3639d"),
"results": [
{
  "ClaimId": "526971f5b5b8b9148404623a",
  "pricingResult": {
    "TxId": 0,
    "ClaimId": "Large_Batch_1",
    "Errors": [

    ],
    "Disposition": [
      {
        "GroupId": 1,
        "PriceAmt": 20,
        "Status": "Priced Successfully",
        "ReasonCode": 0,
        "Reason": "RmbModel(PAM_DC_1):ProgramNode(Validation CPG):ServiceGroupNode(Medical Services):RmbTerm(RT)",
        "PricingMethodologyId": 2,
        "Lines": [
          {
            "Id": 1
          }
        ]
      }
    ]
  }
},

A:

If you want to find objectId's:
import com.mongodb.casbah.Imports._ 
collection.find(MongoDBObject(/*query*/)).map(_._id)

If you want to query by id:
collection.findOneByID(/*id*/)

