ReactiveMongo:使用反应性mongo查询

时间:2015-07-01 06:53:20

标签: mongodb scala mongodb-query reactivemongo

我有以下mongodb文件:

{
"_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
},
-----------
}

当我使用以下查询查询文档时:

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

它只返回company id。但是当我将投影值更改为projection($doc("location" -> 1, "_id" -> 0))时,它会返回空文档。我正在使用Query DSL来编写查询。

更新

当我创建如下的查询时:

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

我的返回值是Company的地图,其属性LocationField使用投影,其余的字段被mongodb忽略。但我的基本要求是仅返回location内部文档值和使用LocationField的地图。当我在mongo db console中运行查询时:

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

结果行为与我的反应性mongo查询相同。是否可以使用mongo db仅返回内部文档,而不是完整的文档结构?

0 个答案:

没有答案