I'm using xhgui and getting this error:
127.0.0.1:27017: no such cmd: aggregate
Stack trace:
/var/www/profiler/src/Xhgui/Profiles.php(172): MongoCollection->aggregate(Array)
Code:
$mongo = new MongoClient($config['db.host'], $config['db.options']);
$db = $mongo->$config['db.db'];
$results = $db->results->aggregate(array(
array('$match' => $match),
array(
'$project' => array(
'date' => $col,
'profile.main()' => 1
)
)
));
Is it possible to fix this error? Maybe need to update mongo extension OR is there any other alternative way?
This appears to actually be a problem with your MongoDB instance itself. Aggregation was introduced in version 2.2 of MongoDB, so if your server is running an older version, it won't understand the aggregate() command.
https://docs.mongodb.org/manual/core/aggregation-introduction/
Related
When i use aggregatein PHP, i get error:
MongoResultException: localhost:27017: The 'cursor' option is
required, except for aggregate with the explain argument
I use mongoDB 3.6 and PHP 5.6
Please see the photo
My Code:
$dbconn = new MongoClient();
$c = $dbconn->selectDB("test")->selectCollection("users");
$ops = array(
array(
'$lookup' => array(
'from' => 'news',
'localField' => '_id',
'foreignField' => 'user_id',
'as' => 'user_docs'
)
)
);
$results = $c->aggregate($ops);
var_dump($results);
For other people who may run into the same problem, here is the solution.
The aggregator command was modified in version 3.6, as indicated in the documentation:
Changed in version 3.4: MongoDB 3.6 removes the use of aggregate command without the cursor option unless the command includes the explain option. Unless you include the explain option, you must specify the cursor option.
In Mongo, you could just add the cursor option without specifying any parameter, as specified in the documentation:
cursor: {}
In PHP, you would need to specify the option like this, new stdClass()corresponding to an empty object '{}' in Mongo :
$results = $c->aggregate($ops, ['cursor' => new \stdClass()]);
Here's how to do it for your example :
$dbconn = new MongoClient();
$c = $dbconn->selectDB("test")->selectCollection("users");
$ops = array(
array(
'$lookup' => array(
'from' => 'news',
'localField' => '_id',
'foreignField' => 'user_id',
'as' => 'user_docs'
)
)
);
$results = $c->aggregate($ops, ['cursor' => new \stdClass()]);
var_dump($results);
If you want to take advantage of calling 'cursor' to add parameters, such as batchSize, you can do it like this :
$results = $c->aggregate($ops, ['cursor' => ['batchSize' => 200]]);
All the parameters are listed in the documentation page linked above.
I'm trying to get a phpdaemon running on ubuntu 16.04 with php5.6.
It runs on ubuntu 1404 with php5.5.9. I tried google of course but I don't know php code at all.
This is the error I'm getting:
PHP Warning: array_key_exists() expects parameter 2 to be array, null given in /../../Service.php on line 79
This is the the actual code on line 79 an a bit below:
if(array_key_exists("session_id", $createSession)) {
// Session ID.
$sessionId = $createSession["session_id"];
Would be great if someone could help me out.
EDIT:
thanks for the quick replies. someone asked for the $createsession. I think this is it, but I am not sure.
still, I'm not sure why it runs on the old version.
// Create and invoke.
$createSession = $this->call_service(P_XMLRPC_URL, "create_session", array(
"requester" => $sale['prkcode'] . '#' . $sale['pdcode'],
"service" => P_SERVICE,
"version" => P_VERSION,
"invoke" => array(
"method" => PRM_INVOKE_METHOD,
"parameters" => array(
"tickets" => array(
$ticket
)
)
)
));
I just updated mongodb to version 3.2 and updated my php driver to the latest. Here is the docs.
They changed almost everything with this new driver and there is not enough documentation I think. I couldn't find how to count, limit and sort the data with php as before.
I found sort method in mongodb website, but there is nothing about it in php docs and source code.
Is there anyone who has dealt with this new driver? How can I sort and limit my query results with php?
To use the new mongo php driver, unlike the old version which just requires mongo.so or mongo.dll to be installed, you will need to get the package mongodb/mongodb through composer:
composer.phar require "mongodb/mongodb=^1.0.0#beta"
Then you can use this example for an initial try:
<?php
require 'vendor/autoload.php'; // include Composer goodies
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$collection = new MongoDB\Collection($manager, "db_name.collection_name");
$result = $collection->find( [ 'id' => '1', 'name' => 'John' ] );
foreach ($result as $entry)
{
echo $entry->id, ': ', $entry->name, "\n";
}
?>
More examples can be found in https://github.com/mongodb/mongo-php-library/blob/master/examples/bulkwrite.php and https://github.com/mongodb/mongo-php-library/blob/master/examples/write.php
The count method can be found in https://github.com/mongodb/mongo-php-library/blob/master/src/Collection.php
sort and limit are the options to add to find() method:
$options = [
'sort' => ['id' => 1],
'limit' => 5
];
$result = $collection->find( [ 'id' => '1', 'name' => 'John' ], $options );
I am sending an object to the php file, I need to save it in MongoDB with a special id which is sent by AJAX
here is the code:
$mongo = new MongoClient();
$editor = $db->editor;
$editor->update(
array( '_id' => $_GET['id'] ),
array( '$set' => json_decode($_POST['data']) )
);
but this code doesn't work
I have looked through a lot of questions familiar to my one here on stackoverflow
so, I really need help, because I can't get it!
thanks =)
Try this
Because the _id at mongo is not a simple string but a Object.
you can get this conclusion by try mongo under command line.
example: db.test.findOne({});
$editor->update(
array(
'_id' => new MongoId($_GET['id'])
),
array(
'$set' => json_decode($_POST['data']
)
);
More PHP MongoClass ID
This question already has an answer here:
Parse error: syntax error, unexpected '[' with php 5.3 [duplicate]
(1 answer)
Closed 8 years ago.
I downloaded and installed Ampps and now I'm using PHP version 5.3.28. When I try to create array i.e.
$foo = ['bar'];
or
$foo = [];
or
$data = [
'ts' => time(),
'ip' => $_SERVER['REMOTE_ADDR'],
'user_id' => #$auth->id,
'method' => $_SERVER['REQUEST_METHOD'],
'uri' => $_SERVER['SCRIPT_NAME'],
'data' => json_encode([
'get' => $_GET,
'post' => $post,
]),
];
etc. I always got error "Parse error: syntax error, unexpected '[' in ...". I didn't change anything. Where is problem and how can I fix it?
Using the syntax [] requires PHP 5.5.0 5.4 and higher, earlier versions have to define arrays as:
$array = array( /* data */);
So, if you wish to use the syntax as exampled. then plan an upgrade to 5.5, otherwise use the alternative method to define an array -- Array Documentation
Try something more explicit like this:
$dataTest = array
(
"ts" => time(),
"ip" => 7
);
echo $dataTest["ts"];
echo $dataTest["ip"];