or operation in yii mongo suite or direct mongo suite - mongodb

I am having a problem with mongo db when used in yii framework. I tried both yii extensions yii mong suite and direct mongo suite. I have trouble implementing or operations in mongo with these extension. I used this code in yii mongo suite but its returning all the values.
$userDetail = $user->findAll(
array(
'$or' => array(
"first_name" => "akash",
"first_name" => "anoop"
)
)
); var_dump($userDetail);die;
I dont know how to use or operation with direct mongo suite. Please help me as soon as possible. Thank You.

With the direct MongoDBSuite, providing it implements MongoDB directly, your $or should look like:
$userDetail = $user->findAll(
array(
'$or' => array(
array("first_name" => "akash"),
array("first_name" => "anoop")
)
)
);

I know this is pretty late but let me know if this helps. I'm using YiiMongoDbSuite. You could check to see if the name is in an array.
$criteria = new EMongoCriteria;
$criteria->addCond("first_name","in",array("akash", "anoop"));
Read more here about using the EMongoCriteria Object: http://bit.ly/1oOFPBJ

Related

Find query with and operator in PHP

Hi i am working on backend of web application & want to find the documents from mongodb database that contain key active_status with value set to both 1 & 2. With mongodb PHP i am confused of how to find with both parameters in single query.
My query was this:
$mongoDb = MongoDbConnector::getCollection("endusers");
$endUserData = $mongoDb->find(array('active_status' => 1, '$and' => array('active_status' => 2)));
I have to fetch the users whose active_status should be 1 & 2. The above query doesnt seems to work. What is the right one for that?
Thanks on advance for quick response.
You have $and the wrong way around. Both arguments need to be included:
$endUserData = $mongoDb->find(array(
'$and' => array(
array( 'active_status' => 1 )
array( 'active_status' => 2 )
)
));
And since that would only make sense when looking for both elements within an array element, then you should instead use $all, which is shorter syntax:
$endUserData = $mongoDb->find(array(
'active_status' => array( '$all' => array(1,2) )
));
I should add that unless you intend to match a document like this:
{ "active_status" => [1,2] }
The you do not in fact want $and at all, but rather you want $or or better yet $in for multiple possible values on the same field:
$endUserData = $mongoDb->find(array(
'active_status' => array( '$in' => array(1,2) )
));
This matches documents like this:
{ "active_status": 1 },
{ "active_status": 2 }

creating web services for iphone and cakephp project

Is there any way to collaborate Cakephp project with Iphone. Like: retrieving json data with cakephp models as WebServices in iphone. The aim behind this question is to use the cakephp code rather then creating WebServices differently.
Your help will be appreciated, Thanks!
Cakephp is a framework that outputs json data as much as like any other php frameworks out there.
You have to write functiosn like there:
public function api_get_release_tracks($release_id = null) {
if($release_id) {
$tracks = $this->Track->find('all', array( //sql queries are better here
'fields' => array('id', 'name', 'duration', 'duration_str',
'sample_audio', 'price', 'local_price'),
'conditions' => array('release_id' => $release_id),
'order' => array('position ASC')
));
$this->set('tracks', $tracks);
$this->set('_serialize', 'tracks');
}
}
Then you can get the json output in this url:
http://localhost:8090/{api name in cofig}/{controller name}/{things after api_ in function name}/{input parameters}.json
read more in here
Also you can alter the routing to change the name
api
to anything in
Configure::write('Routing.prefixes', array('master', 'api'));
in core.php in app/config

How to use the "LIKE" statement in mongodb cakephp

I am using ICHIKAWAY's mongodb driver for cakephp.
One thing that I don't really get is how to perform a "LIKE" statement in cakephp using MONGODB.
I tried this statement:
$users = $this->User->find('all', array('conditions' => array('data_type' => 'user', 'profile.firstname LIKE' => '%'.$string)));
but its not working since "LIKE" is an mysql function.
Thanks for the suggestions.
Use MongoRegex
Mongo DB has a LIKE operator - it's simply a regular expression, to use it:
$users = $this->User->find('all', array(
'conditions' => array(
'data_type' => 'user',
'profile.firstname' => new MongoRegex("/$string/i")
)
));
There's an SQL Compatibilty behavior
The Mongodb driver contains a behavior providing sql syntax compatibility. To use it, simply make sure your model is using this behavior:
class User extends AppModel {
$actsAs = array(
'MongoDB.SqlCompatible'
)
}
And then you can use your query exactly as it appears in the question:
$users = $this->User->find('all', array(
'conditions' => array(
'data_type' => 'user',
'profile.firstname LIKE' => '%'.$string
)
));

MongoDB (via Fuelphp): Adding entries on an Array

I would appreciate the help here. For the purpose of this discussion I have an example here (I will use paste bin for the codes):
http://pastebin.com/VPuyKn6W
I am trying to produce this output:
http://pastebin.com/4iMLacRu
I understand that I need to use $push to make this work. But upon testing, it doesn't seem to do anything. I am following the instructions as prescribed in the docs, but instead of using $Id, I am using user_id for finding the document in the collection. Here is my model:
http://pastebin.com/QB94tbZn
Am I misunderstanding something, or I am not using the $push operator properly, or something to do on how I created the document?
After walking outside, I finally got my answer.
public static function create_mongo()
{
$data = array(
'user_id' => '123895',
'First_Name' => 'John',
'Last_name' => 'Doe',
'sites' => array(
array(
'title' => 'Sankaku Complex',
'site' => 'http://sankakucomples.com'
)
)
);
$db = Fuel\Core\Mongo_Db::instance();
$db->insert('test_collection',$data);
}
sites should be an array carrying an array variable.

How to use $in with the _id

How can i use _id in $in.
array('name' => array(
'$in' => array('name1', 'name2')
))
This is how $in is used in mongodb. But i dont know how to use $in with _id.
I used
array('_id' => array(
'$in' => new MongoId(array(My Ids))
))
But it didn't work. Please let me know if any one knows the answer.
Thanks in advance.
You have to provide an array of MongoId's and not an MongoId of array (which is not possible).
So you have to preprocess you array before query or keep these ids as MongoId
you can use array_map for that, or foreach.