如何使用java在mongo db中获取和更新嵌套文档

时间:2015-08-25 05:22:59

标签: java mongodb

我在mongo db中有如下文档。我想得到并更新具有policyMap等于CostCalculation的文档。这个CostCalculation在数组元素中有数组格式更新,例如' policyName'有' CostCalculationPolicyuserDefine'并设置政策说明' ='新价值'请建议使用java代码来解决这个问题。

我搜索过mongo operartors但是无法得到它。

示例mongo db文档结构。

{
"policyMap" : {
"CostCalculation" : [{
    "policyName" : "CostCalculationPolicyuserDefine",
    "policyDesc" : "Priority user Defined Policy",
    "userDefined" : 1
  },
  {
    "policyName" : "CostCalculationPolicyuserDefine1",
    "policyDesc" : "Priority user Defined Policy",
    "userDefined" : 1
  }]
},
"bsVer" : 2,
"bsFlag" : true,
"crBy" : "xxxxx",
"crDate" : NumberLong("1440138385345"),
"entNm" : "xxxx"
}
{
"policyMap" : {
"CostValue" : [{
    "policyName" : "CostValuePolicyuserDefine",
    "policyDesc" : "Priority user Defined Policy",
    "userDefined" : 1
  },
  {
    "policyName" : "CostCalculationPolicyuserDefine1",
    "policyDesc" : "Priority user Defined Policy",
    "userDefined" : 1
  }]
},
"bsVer" : 2,
"bsFlag" : true,
"crBy" : "xxxxx",
"crDate" : NumberLong("1440138385345"),
"entNm" : "xxxx"
}

我的示例java代码

DBCollection coll = db.getCollection("nestedtest");
BasicDBObject searchDocument = new BasicDBObject();
searchDocument.put( "policyMap ", new BasicDBObject("$exists", new  BasicDBObject("$eq", "CostCalculation")));
        coll.remove(searchDocument);

获得正确结果的类似java代码是什么。

感谢。

0 个答案:

没有答案