PHP Mongo query not returning expected results - php

I'm using the latest php mongo driver along with the latest mongodb 2.0. I'm trying to run a base query of host=x return the results then refine the search with other terms.
It's not returning any valid results.
I was thinking something like this, but its obviously not working:
$basefilter = array('host' => new MongoRegex("/1.1.1.1|2.2.2.2/i"));
$filter = array('host' => new MongoRegex("/2.2.2.2/i"));
$basereturn = $collection->find($basefilter);
$initreturn = $basereturn->find($filter);
$return = $initreturn->sort(array('date' => -1))->limit($limit)->skip($skip);
I want to just be able to refine my search. How can this be done?

You can't run a find over a cursor. find only works over collections. I suspect the above fatals out. You can map/reduce into a collection and run find on it.

you probably should scape your regex you have there since the dot is an operator in regex, so that should have been:
$basefilter = array('host' => new MongoRegex("/(1\.1\.1\.1)|(2\.2\.2\.2)/i"));

Related

using MatchAll in elasticsearch and elastica

I am having a hard time trying to use MatchAll in elastic search using elastica, currently I have the following querystring:
$pictureQuery = new \Elastica\Query\QueryString();
$pictureQuery->setParam('query', $searchquery);
$pictureQuery->setParam('fields', array(
'caption'
));
$items = $itemFinder->find($pictureQuery);
the issue with this query is that it only returns 10 results. I wanted to return all results, in this case MatchAll. I am however having issues on how to get all matching results, how do I do so?
Elasticsearch returns by default the top 10 results (the more relevant).
That's the expected behavior.
Elasticsearch allows to change page size (size) and change page (from). Have a look at From/Size API.
In Elastica, I guess it's here: http://elastica.io/api/classes/Elastica.Query.html#method_setSize

mongodb conversation system

I'm implementing a very simple conversation system on mongodb.
The idea should be that when I'm opening a convo, it should display send and received messages. It's OK so far and should be pretty easy, by using a simple query like this pseudocode:
(from "my_id" AND to "friend_id") OR (from "friend_id" AND to "my_id")
this should be pretty straightforward and simple, but querying just looks so complicated to me with mongodb (I'm coming from mysql).
I'm trying this, but it's not working at all, and can't find out where the error is.
$cursor =$collection->find
(
array('$or' =>
array('$and' => array("from"=>"$profile", "to"=>"$loggeduser")),
array('$and' => array("to"=>"$profile", "from"=>"$loggeduser"))
)
)->limit(50)->sort(array('date' => -1));
this returns nothing.... Where's the mistake?
Thanks in advance.
Take a look at this page on how to do advanced MongoDB queries: http://www.mongodb.org/display/DOCS/Advanced+Queries
You can use a combination of the $and and $in operators to get what you need. Using the mongo shell, your query would look something like this:
db.yourCollectionName.find({$and: {from: {$in: ["toUser", "loggedOnUser"]}}, {to: {$in: ["toUser", "loggedOnUser"]}}})
I believe this may also give you the equivalent:
db.yourCollectionName.find({$and: {$or: [{from: "toUser"}, {to: "toUser"}]}}, {$or: [{from: "loggedOnUser"}, {to: "loggedOnUser"}]}}})
From there it's a matter of converting the above to the language/DSL that you're using, and sorting by date.
In your code, you don't need the ($and => array()) wrapping each of the objects that you're trying to find. Remove them, so it looks like this:
$cursor = $collection->find(
array('$or' =>
array(
array("from"=>"$profile", "to"=>"$loggeduser"),
array("to"=>"$profile", "from"=>"$loggeduser")
)
)
) ->limit(50)->sort(array('date' => -1));

Filter Mongo cursor fields to desired array index with PHP

In the process of learning to use Mongo and PHP, and got stuck with this.
I have a collection in Mongo with the following structure:
array(3){
["timehack"]=>int(..),
["_id"]=>object..,
["series"]=>
array(3){
[0]=>int(..),
[1]=>int(..),
[2]=>int(..)
}
}
I am trying to query the collection so i get all of the samples with first item in "series", something like:
$cursor = $collection->find();
$cursor->fields(array=>("timehack"=>true, "_id"=>false, "series.0"=>true));
$arr = $cursor->getNext();
var_dump($arr);
The resulting series array is empty. How can I get just the desired index? (I realize that I can get all samples in the series then filter with code, but I would like to know how to accomplish this with a query). Thanks.
In the comments above you have already confirmed that this works with the $slice operator in the shell. So, to give you an example of how $slice works in PHP, I refer you to none other than the PHP tests in the driver itself.
Take a look at line 33 in the tests/MongoCollectionTest2.php file on github:
https://github.com/mongodb/mongo-php-driver/blob/master/tests/MongoCollectionTest2.php
The code goes like this:
$m = new Mongo();
$db = new MongoDB($m, "phpunit");
$this->object = $db->selectCollection('c');
...
...
$results = $this->object->find(array(),array("x" => array('$slice' => 3)))->getNext();
It looks a little convoluted compared to the shell with a lot of casting as an object going on (if I am reading that right), but hopefully enough to get you started :)

MongoDB PHP query using $and operator on the same object

I've got a fairly simple query that I have working in command line, and am trying to execute using php.
It's looking for documents that match all of the given "tags" entered in a search box:
db.collection.find( { $and: [ { tags: "cats" }, { tags: "video" } ] } )
I can't seem to figure out how to translate this to php. I've been using codeigniter for everything up to this point (Alex Bilbie's library), but have looked into building my own queries with no luck. Most of the methods I've tried eliminate the first tag (cats), since it is looking at the same field name (tags).
any thoughts?
PHP can be a bit tricky with how you need to format the arrays. What I've found to be the best way to create the queries is through doing things like:
json_encode($myQuery);
then comparing that to what actually works directly on the console of the app. In this case you're looking for:
$item = array('$and' => array(array('tags' => 'cats'), array('tags' => 'videos')))
which you can confirm by doing:
echo(json_encode(array('$and' => array(array('tags' => 'cats'), array('tags' => 'videos')))));
Good Luck!

Perform case-insensitive lookup on an Array in MongoDB?

So, I've decided to get my feet wet with MongoDB and love it so far. It seems very fast and flexible which is great. But, I'm still going through the initial learning curve and as such, I'm spending hours digging for info on the most basic things.
I've search throughout the MongoDB online documentation and have spent hours Googling through pages without any mention of this. I know Mongo is still quite new (v1.x) so it explains why there isn't much information yet. I've even trying looking for books on Mongo without much luck. So yes, I've tried to RTM with no luck, so, now I turn to you.
I have an Array of various Hashtags nested in each document (ie: #apples, #oranges, #Apples, #APPLES) and I would like to perform a case-insensitive find() to access all the documents containing apples in any case. It seems that find does support some regex with /i, but I can't seem to get this working either.
Anyway, I hope this is a quick answer for someone.
Here's my existing call in PHP which is case sensitive:
$cursor = $collection->find(array( "hashtags" => array("#".$keyword)))->sort(array('$natural' => -1))->limit(10);
Help?
I suspect your query is not returning what you really want...
If you do
db.col.find( { some_array_field: ["item1", "item2"] } );
then it will only match documents that have EXACTLY these two items in some_array_field. So if there is a document with [item1, item2] hashtags it will match, but a document with [item1, item2, item3] tags won't match.
You could use the $all argument as described in this post:
How do you do an AND query on an array in mongodb?
e.g.
db.col.find( { some_array_field: { $all : ["item1", "item2"] } } );
or:
db.col.find( { some_array_field: "item1", some_array_field: "item2" } );
This distinction of complete-document-match and partial-match was really confusing in MongoDB for me at first.
here is an example for case insensitive search in mongodb with php
$search_string='baR';
$searchQuery = array(
'$or' => array(
array(
'field1' => new MongoRegex("/^$search_string/i"),
),
array(
'field2' => new MongoRegex("/^$search_string/i"),
),
)
);
$cursor = $customers->find($searchQuery);
Hopes this help any.

Categories