-1
$StringJsonVariable = '{"data": {"Parent": { "abc": [{"value": "10","code": "c102"},{"value": "20","code": "c103" }]}}';
$JsonVariable = json_decode($StringJsonVariable,true);
$connection = new MongoClient();
$db = $connection -> dbvalue;
$collection = $db -> collVal;
foreach($JsonVariable['data']['Parent'] as $key => $value[0]){
$val=$value[0];
$collection->update(array('_id'=>"abc"),array('$addToSet' => array('VALUES' => array('$each' =>$val), array('upsert' => true))));}
试过使用addtoSet每个使用上面的语法。我想要插入
单个呼叫。如何使用phpMongo将整个Unique文档插入到MongoDB中的数组中?
Upserts和添加值以阵列是基本上在单个操作中互相排斥,除非你的数据补充的是一个“真正的集合”,这是完全唯一的。你没有说明你的实际问题是什么,但我怀疑'$ addToSet'没有做你想做的事,因为只有一个字段被认为是唯一的,而不是数组的所有内容。这里有几个答案(我知道,因为我已经写了几个),基本上说,如果你想要不是所有属性的独特元素,那么你需要多个操作。批量操作提供一个请求,但不是一个操作。 –