-2

I have a documents in mongo db as follows. I want to get and update the document having policyMap equals CostCalculation.In this CostCalculation has array format update in array elements such as 'policyName' have 'CostCalculationPolicyuserDefine' and set 'policyDes' = 'New Value' Please suggest the java code to solve this.

I searched mongo operartors but couldn't get it.

Sample mongo db document structure.

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

My sample java code

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

What would be the similar java code to get the correct result.

Thanks.

karthik.A.M
  • 115
  • 2
  • 2
  • 11

2 Answers2

0

This code works using Mongo 3 java drivers. It removes the need for all the BasicDBObjects that are in your code:

    MongoClientURI uri = new MongoClientURI("mongodb://localhost:27017");
    MongoClient client = new MongoClient(uri);
    initialiseCollection(client);

    Bson filter = Filters.exists("policyMap.costCalculation");
    client.getDatabase("test").getCollection("test").deleteOne(filter);
robjwilkins
  • 5,462
  • 5
  • 43
  • 59
-1
DBCollection coll = db.getCollection("minnalPolicyMetadata");
BasicDBObject searchDocument = new BasicDBObject();
searchDocument.put("policyMap.CostCalculation",new BasicDBObject("$exists",true));
DBCursor cursor = coll.find(searchDocument);                    
if(cursor.count() != 0){
while(cursor.hasNext())
System.out.println(cursor.next());

                    }

In this way i solved this problem.Use exists operator in mongo

karthik.A.M
  • 115
  • 2
  • 2
  • 11
  • This code is a mess. Its poorly indented, has commented out lines, System.out.println statements in it, and doesnt appear to address the issue from the original question – robjwilkins Aug 25 '15 at 09:09
  • In this using i'm using search operation.If you want to delete that record use col1.remove(condition). – karthik.A.M Aug 25 '15 at 09:20
  • How to do update in this document can any one help me please @robjwilkins – karthik.A.M Aug 26 '15 at 09:31