MongoDB Upsert无法在PHP中运行

时间:2016-04-07 16:15:15

标签: php mongodb

我正在尝试对我的文档运行更新,我正在使用upsert true但它仍然会被覆盖?

$col = "A" . $user->agencyID;
$db = $m->rules;
$collection = $db->$col;

$validValue = $_POST['validValue'];
$id = $_POST['ruleID'];

$document = array(
    'tags' => array(
                $validValue
              )
  );


$collection->update(
    array(
      '_id' => new MongoId($id)
      ),
    array('$set' => $document),
    array('upsert'=>true)
);

$validValue就像 - Foo Bar

第一个值很好但是当我尝试添加不同的值时会覆盖第一个值吗?

2 个答案:

答案 0 :(得分:1)

我设法找出了问题,我需要$addToSet并且还需要从我的.directive('htmlEditor', function ($window) { return { restrict: 'A', require: '?ngModel', scope: {}, link: function (scope, element, attrs, ngModel) { var ele = element[0]; var init = function () { if (!window.CKEDITOR || !window.ckinited) { var scriptTag = document.createElement('script'); scriptTag.onload = handleScriptLoad; scriptTag.src = '//cdn.ckeditor.com/4.5.8/full/ckeditor.js'; $window.document.head.appendChild(scriptTag); window.ckinited = true; } else { handleScriptLoad(); } }; var handleScriptLoad = function () { var config = { allowedContent: true, toolbar: [ {name: 'basicstyles', items: ['Bold', 'Italic', 'Underline', '-', 'RemoveFormat']}, {name: 'paragraph', items: ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent']}, {name: 'text-align', items: ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock']}, {name: 'styles', items: ['Font', 'FontSize']}, {name: 'colors', items: ['TextColor', 'BGColor']}, {name: 'tools', items: ['Table', 'HorizontalRule', 'Maximize']}, ] }; ele.setAttribute('contenteditable', true); var editor = window.CKEDITOR.replace(ele, config); editor.setData(ngModel.$viewValue); editor.on('change', function (evt) { ngModel.$setViewValue(evt.editor.getData()); }); editor.on( 'dragstart', function( evt ) { console.log("something"); }); editor.on( 'dragenter', function( evt ) { console.log("something"); }); editor.on( 'dragover', function( evt ) { console.log("something"); }); ngModel.$render = function () { editor.setData(ngModel.$viewValue); }; }; init(); } }; });

周围获取数组()

答案 1 :(得分:1)

实际上,使用$addToSet,如果数值已存在,则不会将值推入数组。此代码未经测试,请更改以满足您的需求。

$col = "A" . $user->agencyID;
$db = $m->rules;
$collection = $db->$col;

$validValue = $_POST['validValue'];
$id = $_POST['ruleID'];

$document = array(
    'tags' => array(
            $validValue
    )
);


$collection->update(
    array(
      '_id' => new MongoId($id)
    ),
    array('$addToSet' => array('tags' => $document))
);