문제

I'm having a bit of a problem performing an upsert in Lithium with MongoDB. The update works fine but it won't do the insert. My code is:

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)
);

Which should correspond to the MongoDB query:

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

Neither query nor PHP code is working. Is their an alternative to $addToSet that won't allow duplicates in the array of user_ids?

(Sorry I'm new to both Lithium and Mongo

도움이 되었습니까?

해결책

There seem to be problems with the first two arguments to update(). Those arguments are for the data and conditions, so you don't need those keys in the arrays you are passing. Try this.

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

I'm not sure why the query doesn't work for you directly in MongoDB. That works fine for me.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top