dongnao2048 2016-06-04 21:10
浏览 52
已采纳

upsert脚本无效

I'm trying to get upsert to work and getting no results

I have three variables I'm posting before this script. If I update, I get result, but I want to upsert and am getting no results.

Here is my code:

if(isset($_POST)){
    if($array){
        $doc = $collection->update(
            array(
                '$set' => array(
                    '_id' => new MongoId(),
                    'organization' => $mysql_org_name,
                    'purch_code' => $mysql_purch_code,
                    'contentname' => $mysql_order_item_name,
                    array('upsert' => true)
                )
            )
        );
    }
}
  • 写回答

2条回答 默认 最新

  • duanhui1185 2016-06-04 21:28
    关注

    Update expects 3 argument arrays. You've only passed one.

    First, you've included you options as part of the $set array. They should be passed as a separate argument after the update clause.

    Also, you don't have a match criteria (i.e., a query statement to select which item(s) to update). When using upsert and $set, a new item would be created with the fields from both arrays(see the relevant documentation).

    Finally, if you don't specify a value for _id, Mongo will generate one for you.


    If, for instance, you wanted to match based on the organization field:

    if(isset($_POST)){
        if($array){
            $doc = $collection->update(
                array('organization' => $mysql_org_name),
                array(
                    '$set' => array(
                        'purch_code' => $mysql_purch_code,
                        'contentname' => $mysql_order_item_name,
                    )
                ),
                array('upsert' => true)
            );
        }
    }
    

    If this matches one or more entries, only the purch_code and contentname fields will be updated on the first matching object (to update multiple entries, you'd have to specify 'multiple'=>true in your options array). If nothing matches, a new entry would be inserted with:

    1. A generated _id
    2. All of the match criteria values (i.e., organization)
    3. All of the update operator values (i.e., purch_code and contentname)
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向