Suppose my data is like this:
([{
attribute: 'amount'
rank: 1
value: '500'
rules:
[{
name:'R1',
parent: 'true',
successor_attr: 'project',
},
name:'R2',
parent: 'true',
successor_attr: 'type',
}]
})]
Now, in the list rules, I want to add an object if it's not present or else leave it as it is, basically an 'upsert' operation. To achieve the same, I tried:
BasicDBObject criteria = new BasicDBObject();
criteria.append("attribute", doc.getString("attribute"));
criteria.append("value", doc.getString("value"));
criteria.append("rank", doc.getInteger("rank"));
criteria.append("rules.name", list.getString("name"));
criteria.append("rules.parent", list.getString("parent"));
criteria.append("rules.successor_attr", list.getString("attribute"));
//To add the object if not present already.
BasicDBObject docdetail = new BasicDBObject();
docdetail.put("name", list.getString("name"));
docdetail.put("parent", list.getString("parent"));
docdetail.put("successor_attr", list.getString("attribute"));
BasicDBObject modifiedObject = new BasicDBObject();
modifiedObject.put("$push", new BasicDBObject().append("rules",docdetail));
collection1.update(criteria,modifiedObject,true,false);
When trying to do the same, I'm getting the error:
Write failed with error code 2 and error message 'The field 'rules' must be an array but is of type object in document {no id}'
The error is at the last line (collection1.update...). Please suggest how can I remove this error and achieve the functionality I want.