在Lithium中使用MongoDB upsert

Daw*_*ell 1 mongodb lithium

我在使用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)
);
Run Code Online (Sandbox Code Playgroud)

哪个应该对应MongoDB查询:

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

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

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

rma*_*her 5

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

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

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