The contents of my collection are in this form,
{ "_id" : ObjectId("50535036381ef82c08000002"),"source_references" : [
{
"_id" : ObjectId("50535036381ef82c08000001"),
"name" : "abc",
"key" : "123"
}]
}
Now I want to insert another array in the "source_references" if the name and key does not exist in the nested array else don't insert. Here is the result I want,
{ "_id" : ObjectId("50535036381ef82c08000002"),"source_references" : [
{
"_id" : ObjectId("50535036381ef82c08000001"),
"name" : "abc",
"key" : "123"
}
{
"_id" : ObjectId("50535036381ef82c08000003"),
"name" : "reuters",
"key" : "139215"
}]
}
Here is what I've tried :
$Update_tag = array('$addToSet' => array("source_references.$" => array("name" => "reuters", "key" => $r_id)));
$mycollection->update(array("_id" => $id), $Update_tag);
But I am not able to insert another array inside the nested array. Also I want to create the "_id" field(inside nested array) only if new array inserted in source_references.
Where I am going wrong? Hope I am clear with my question.
soruce_references.$
wrong when creating the array, and you have duplicate_id
's (reuters & the root) in the expected result. – Joachim Isaksson Sep 15 '12 at 6:53