首页 文章

在Lithium中使用MongoDB upsert

提问于
浏览
1

我在使用MongoDB在Lithium中执行upsert时遇到了一些问题 . 更新工作正常,但它不会执行插入 . 我的代码是:

Feeds::update(
    array('data' =>
        array('user_id' => 
            array('$addToSet' => $user["_id"]) 
        )
    ), //what to change
    array('conditions' => 
        array('address' => $address)
    ), //where to change it
    array('upsert' => true)
);

哪个应该对应MongoDB查询:

db.osmblags.update( { "address" : "test address" }, { $addToSet : { "user_id" : "56" } }, { upsert : true } );

查询和PHP代码都不起作用 . 他们是$ addToSet的替代品,不允许在user_ids数组中重复吗?

(对不起,我是Lithium和Mongo的新手

1 回答

  • 5

    update() 的前两个参数似乎存在问题 . 这些参数是针对数据和条件的,因此您不需要传递的数组中的那些键 . 试试这个 .

    Feeds::update(
        array('$addToSet' => 
            array('user_id' => $user["_id"]) 
        ), //what to change
        array('address' => $address), //where to change it
        array('upsert' => true)
    );
    

    我不确定为什么查询不能直接在MongoDB中使用 . 这对我来说很好 .

相关问题