Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top