我非常渴望开始使用PHP 7但是有一个问题正在阻碍...我主要使用MongoDB作为数据库,问题是我不知道如何为PHP 7安装MongoDB驱动程序/客户端.
我目前的安装是PHP 5.6和我的Mac brew install php56-mongo上的诀窍.
任何人都可以推荐我如何在我的Mac或Ubuntu安装上工作?
在此先感谢,非常感谢!
我已经更新了mongo,现在在日志中出现以下错误: 不推荐使用不带游标选项的aggregate命令
Mongo说我应该将第二个REQUIRED参数添加到聚合函数中,因为我当前的用法已被弃用.
我目前使用以下代码PHP(现已弃用):
$this->db->{$collection}->aggregate($options);
Run Code Online (Sandbox Code Playgroud)
并返回以下格式:
{"result":[
{
"_id":"xxxxxx",
"update":[
{
"firstUpdateTime":xxxxxx,
"updateTime":xxxxxxx,
}
],
"media":[
{
"xxxx":{ ...
Run Code Online (Sandbox Code Playgroud)
为了不使用弃用的代码,我添加了新的第二个参数(但我不明白要放什么):
$this->db->{$collection}->aggregate($options, array('cursor' => array('batchSize' => 101)));
Run Code Online (Sandbox Code Playgroud)
这会返回相同的信息,但会改变初始结构:
{"cursor":{
"id":{
"value":"xxxxxx"
},
"ns":"xxxxxx.instagram",
"firstBatch":[
{
"_id":"xxxxxx",
"update":[
{
"firstUpdateTime":xxxxxx,
"updateTime":xxxxxx,
}
],
"media":[
{
"xxxxxx":{ ...
Run Code Online (Sandbox Code Playgroud)
更新后,Mongo强迫我改变读取数据的方式.我不明白我应该把第二个参数叫做"游标"...
我应该在第二个参数中加入什么?我可以在不改变结果结构的情况下设置默认值吗?
Doc:https : //docs.mongodb.com/manual/reference/method/db.collection.aggregate/ http://php.net/manual/es/mongocollection.aggregate.php
更新:
如果我在函数中指示光标我不再收到错误.但是,如果没有应用解决方案,我会阅读LOG并随机出现警告,我有一个代码,我运行了几次,有时如果报告上述警告而其他人没有.
为什么?
我TblStudent在 mongodb 中有一个集合
{
"_id": ObjectId("5baa85041d7859f40d000029"),
"Name": "John Doe",
"RollNo": 12,
"Class": "Ist"
....
}
Run Code Online (Sandbox Code Playgroud)
我还有一个TblRoute像
{
"_id": ObjectId("5baa818d1d78594010000029"),
"Name": "New york City",
"StopDetails": [
{
"StopId": "abc777",
"Name": "Block no 3"
},
{
"StopId": "abc888",
"Name": "Block no 4"
}
],
"NumberOfSeats": "10",
"StudentDetails": [
{
"StudentId": ObjectId("5baa85041d7859f40d000029"),
"VehicleId": "7756"
},
{
"StudentId": ObjectId("5baa85f61d7859401000002a"),
"VehicleId": "7676"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 mongodb 3.6 平台。我正在使用以下代码行
$query = ['_id' => new MongoDB\BSON\ObjectID($this->id)];
$cursor = $this->db->TblRoute->aggregate([
['$match' => $query], …Run Code Online (Sandbox Code Playgroud) aggregate-functions mongodb mongodb-php aggregation-framework php-mongodb
我使用PHP版本5.3,所以我需要下载MongoDB 3.0和PHP驱动程序版本1.6如何下载旧版本(3.0)的MongoDB和PHP驱动程序..而且我也想使用PHP编写器,我已经使用过Composer for like composer require "mongodb/mongodb=^1.0.0"(对于PHP驱动程序版本1.0),现在我想使用PHP编译器为PHP驱动程序版本1.6 ..
composer require "mongodb/mongodb=^1.6.0" 可能吗?
请为MongoDB3.0建议您的解决方案和发布下载链接
我无法连接到副本集.
[MongoDB\Driver\Exception\ConnectionTimeoutException]
No suitable servers found (`serverSelectionTryOnce` set):
[Server closed connection. calling ismaster on 'a.mongodb.net:27017']
[Server closed connection. calling ismaster on 'b.mongodb.net:27017']
[Server closed connection. calling ismaster on 'c.mongodb.net:27017']
Run Code Online (Sandbox Code Playgroud)
但是,我可以使用MongoChef进行连接
当我在 PHP7 中使用驱动程序MongoDB\Driver时,php 总是与 mongo 创建大量连接。而且我自己无法关闭连接,因为 MongoDB\Driver 没有 close() 方法。
如何禁用持久连接?或者怎么配置?
我试图用 limit 和skip 来限制查询返回长度,但返回的数组是一个空数组。这是代码片段,
$start = 0;
$limit = 10;
$options = [
'skip' => $start,
'limit' => $limit,
];
$return = $db->collection->find([], $options);
Run Code Online (Sandbox Code Playgroud)
我做错了什么吗?这是一个错误还是随机的事情?
问候
PS:我知道这是一个已经被问到的问题,但大多数答案都与旧的扩展有关。
将MongoDB与PHP的MongoDB驱动程序一起使用我无法使用正则表达式过滤搜索结果.在手册中没有给出如何使用"过滤器"选项的示例:MongoDB\Driver\Query.
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$filter = array(?????);
$options = array("projection" => array("fieldname" => 1));
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery("dbname.collectionname", $query);
foreach($cursor as $document) {
var_dump($document);
}
Run Code Online (Sandbox Code Playgroud)
我尝试了大约20种不同的可能性,但找不到答案.没有正则表达式的查询工作正常.
我必须在 php 中使用 Mongodb,并尝试使用 php 从 mongocollection 获取数据。带有php返回记录的后续mongoquery成功。但我想为以下查询设置限制。
PHP代码:
$query = array("\$and"=>array(array('fld'=> array("\$in"=> array('4', '14', '20'))), array('stat'=>array("\$eq"=>"A"))));
$cursor = $this->collection->find($query);
Run Code Online (Sandbox Code Playgroud)
我也试过以下方式
$query = array("\$and"=>array(array('fld'=> array("\$in"=> array('4', '14', '20'))), array('stat'=>array("\$eq"=>"A")))).limit(2);
Run Code Online (Sandbox Code Playgroud)
但我遇到了致命错误
调用未定义的函数 limit()
如何limit()在上述查询中使用?
我找了一晚上,也没找到解决办法。我尝试通过运行来安装 mongodb 扩展sudo /Applications/XAMPP/xamppfiles/bin/pecl install mongodb
,但最后我收到此错误
fatal error: 'unicode/usprep.h' file not found
#include <unicode/usprep.h>
^~~~~~~~~~~~~~~~~~
1 error generated.
make: *** [src/libmongoc/src/libmongoc/src/mongoc/mongoc-scram.lo]
Error 1
ERROR: `make' failed
Run Code Online (Sandbox Code Playgroud)