2

I my project, I get mongodb data using casbah.

    val mongoClient = MongoClient()
    val db = mongoClient("configServerTest")
    val coll = db(moduleName)
    val allDocs = coll.find()
    allDocs.foreach(allDocs => println(allDocs))

The result is

{ "_id" : "version" , "data" : "0.0.1"}
{ "_id" : "portNum" , "data" : 7001}
{ "_id" : "url" , "data" : "http://localhost"}

But I need only to get _id names as a list. How can I do it using casbah ?

("version","portNum","url")
Shashika
  • 1,606
  • 6
  • 28
  • 47

1 Answers1

0

You can find this from the api doc.

def find[A, B](ref: A, keys: B)(
    implicit arg0: (A) ⇒ commons.TypeImports.DBObject, 
    arg1: (B) ⇒ commons.TypeImports.DBObject): CursorType

Queries for an object in this collection.

An empty DBObject will match every document in the collection. Regardless of fields specified, the _id fields are always returned.

so

val allDocs = coll.find(new BasicDBObject(), new BasicDBObject())
Tim Green
  • 3,571
  • 2
  • 23
  • 23
  • It's worked in this way ` val query = MongoDBObject("_id" -> 1) val allDocs = coll.find(new BasicDBObject(), query)` – Shashika Feb 26 '14 at 05:37
  • To clarify that, the **second** document is the [Projection](http://docs.mongodb.org/manual/reference/method/db.collection.find/#projections) where you define the fields you want to return. – Neil Lunn Feb 26 '14 at 05:37
  • @Shashika and actually if your **projection** contains `{ _id: 0 }` then the `_id` field **is** omitted. – Neil Lunn Feb 26 '14 at 05:39