0

i have following mongodb document:

{
"_id" : ObjectId("5592c0e6ea16e552ac90e169"),
-----------
"location" : {
    "_id" : ObjectId("5592c17fc3ad8cbffa0e9778"),
    "companyFieldId" : ObjectId("559140f1ea16e552ac90e058"),
    "name" : "Location",
    "alias" : "Points",
    "locations" : [ 
        {
            "_id" : ObjectId("5592c17fc3ad8cbffa0e9779"),
            "country" : "India",
            "state" : "Punjab",
            "city" : "Moga",
            "zip" : "142001",
            "custom" : false
        }, 
        {
            "_id" : ObjectId("5592c17fc3ad8cbffa0e977a"),
            "country" : "India da address",
            "custom" : true
        }
    ],
    "mandatory" : true,
    "allowForGroups" : false
},
-----------
}

When i query the document using following query:

companyCollection.find($doc("_id" $eq companyId, "location._id" $eq locationId)).projection($doc("location" -> 1, "_id" -> 1)).cursor[LocationField].headOption;

It will return only company id. But when i change the projection value to projection($doc("location" -> 1, "_id" -> 0)) it return empty document. I am using Query DSL for write the queries.

UPDATE

When i create query like:

companyCollection.find($doc("_id" $eq companyId, "department._id" $eq departmentId), $doc("department" -> 1, "_id" -> 0)).cursor[Company].headOption 

with this my return value is map with Company and with its property LocationField using projection and rest of the fields are ignore by mongodb. But my basic requirement is return only location inner document value and map with LocationField. When i run query in mongo db console like :

db.companies.find({"_id": ObjectId('5592c0e6ea16e552ac90e169'), "location._id": ObjectId('5592c17fc3ad8cbffa0e9778')}, {"location": 1, "_id": 0})

The result behavior is same as my reactive mongo query. Is it possible with mongo db for return only inner document, instead of full document structure?

Harmeet Singh Taara
  • 6,483
  • 20
  • 73
  • 126
  • Try without the DSL. Note that this extension is not compatible with 0.11. – cchantep Jul 01 '15 at 10:53
  • The [unit tests](https://github.com/ReactiveMongo/ReactiveMongo/blob/0.10.5.x.akka23/driver/src/test/scala/BSONCollectionSpec.scala#L66) of the driver (versions 0.10.5.x and 0.11.0) confirm that the projection document is used. – cchantep Jul 01 '15 at 11:18
  • hello @cchantep i am using `"org.reactivemongo" % "reactivemongo_2.11" % "0.10.5.0.akka23"` and with this, query DSL is possible with reactive mongo and it is run as i mention in above update section. – Harmeet Singh Taara Jul 01 '15 at 13:49
  • As mentioned just before, the unit tests of the driver, 0.10.5.x or 0.11.0 indicate that the project document is properly used. So I suggest you try to test your query without the DSL, with plain `BSONDocument`. – cchantep Jul 01 '15 at 14:51

0 Answers0