My mongoDB have field: $creatime, I want add field to function php.
How do it?
$ops = array(
array('$match' => $query),
array('$addFields' => array('date_code' => functionPHP('$create_time'))),
);
$results = $col->aggregate($ops,array('cursor' => array('batchSize' => 200)));
Related
I have an api with one optional parameter called limit, which takes an integer and limits the number of documents returned from the api in a get request.
Implementing this limit is fine in my PHP application when it is a required parameter, but when its not specified as part of my get request, what is the best way to handle it?
is there for example, a way to define $limit and set it to all documents? (in pseudo code, $limit = none)
$list = $collection->aggregate( array( array('$match' => array( ... )),
'$project' => array ( ... )), '$limit' => intval($this->limit) ));
$this->limit //this is the optional input parameter
As you can see above, when the limit parameter is required it functions as required. Now when its ommitted, how can I keep the above code but specify no limit?
You could manually generate your where clause.
// Declare a where clause with your fixed conditions
$whereClause = array(array('$match' => array(...)), array('$project' => array(...)));
// Check if there's a limit
if($this->limit != 0)
array_push($whereClause, array('$limit' => $this->limit));
// Finally, call with the where clause we've generated above
$list = $collection->aggregate($whereClause);
Short answer No!
But, you can use an if/else condition like this:
<?php
if(intval($this->limit) != 0 ) {
$list = $collection->aggregate( array(
array('$match' => array( ... )),
array('$project' => array ( ... ))
array('$limit' => intval($this->limit))
);
));
} else {
$list = $collection->aggregate( array(
array('$match' => array( ... )),
array('$project' => array ( ... )),
);
}
?>
I'm using mongodb 2.4 and added fulltext index to the "title" field in one the collection. How should I search something in that field using php?
This is the code I use right now:
$params = array(
'_id' => array(
'$gt' => (int)$gt
)
);
$r = $this->collection->find( $params )->limit($limit);
This seem to be the answer to my question:
<?php
$result = $db->command(
array(
'text' => 'bar', //this is the name of the collection where we are searching
'search' => 'hotel', //the string to search
'limit' => 5, //the number of results, by default is 1000
'project' => Array( //the fields to retrieve from db
'title' => 1
)
)
);
http://www.php.net/manual/en/mongodb.command.php#111891
i have the array here.
http://pastebin.com/i5ZUQNm6
and this from php result.
$result1 = $mo->find(
array(
'username' => 'BLABLA',
'stream' => array('$exists' => true)
)
);
foreach ($result1 as $obj) {
print_r($obj);
}
how i sort the [stream] child and limit it to 1 result? and how to find [id_stream] child?
thanks
To limit, to a single document
use limit(1):
$result1 = $mo->find( array(
'username' => 'BLABLA',
'stream' => array('$exists' => true) ) )->limit(1);
or
findOne
$result1 = $mo->findOne( array(
'username' => 'BLABLA',
'stream' => array('$exists' => true) ) );
And to sort the child.
You need to also add at the end ->sort(array("stream.time" => -1));
I've seen the pastebin, and I guess you want to order by date.
Final code:
$result1 = $mo->findOne( array(
'username' => 'BLABLA',
'stream' => array('$exists' => true)))->sort(array("stream.time" => -1));;
From what I know for limiting to a single value of the array you need a loop
you need to add (inside the find, just after the main array): find(.... , array('streams' => array('$slice' => 1)));
Using a combination of sort and limit should work - the code in slownage's answer looks like it may be what you need.
Is there any kind of php library to execute SQL queries agains arrays?
Something like this:
$array = array(
array(
"field_a" => "first_a"
"field_b" => "something"
),
array(
"field_a" => "second_a"
"field_b" => "else"
),
array(
"field_a" => "third_a"
"field_b" => "something"
)
);
$rows = arrayQuery($array, "SELECT field_a WHERE field_b = 'something'");
/*
$rows = array(
array(
"field_a" => "first_a"
),
array(
"field_a" => "third_a"
)
);
*/
Found something similar: http://blog.phpdeveloper.org/?p=61, but that is not a query.
I believe that LINQ for PHP would do the trick. I think this is a close as it gets to 'querying arrays' in PHP.
is it possible to find all records in an collection where the MongoID is not in
an provided array?
Something like this (?):
$search = array(
'_id' => array('$ne' => $ids)
'readby' => array('$ne' => $userId) // works
);
Iam using PHP with the Mongo Extension.
Use $nin instead of $ne with arrays. Something like:
$search = array(
'_id' => array('$nin' => $ids),
'readby' => array('$ne' => $userId)
);
should do what you want.