We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Help setting up MongoDB

I could do with a little help setting up mongoDB with Phalcon to output a simple find

The system I have is as follows:

  • ubuntu 17.04
  • php 7.022
  • Phalcon v3.2.2

(New 'MongoDB' not old 'Mongo' type)

  • MongoDB Server V3.4.7
  • MongoDB extension v1.2.10 (stable)
  • MongoDB Driver (I think v1.2 via composer)
  • New install of Incubator via composer

I can use the php driver to get the result I need but I cannot do it the phalcon way.

I have tried for over a week to get anything to work, spent hours on google reading old and new posts about getting MongoDB to work and have completely destroyed numerous VM installations in the process.

To make things really simple and easy to understand what is needed, and what is happening a simple test script without models and controllers just the absolute basics needed for it to work would be a great help from anyone.

It appears I am not the only one with this issue and I would rather not use the old deprecated extension when it sounds like some have got the new Mongodb extension working ok.

i.e.

<?php

use or include needed files/classes

connect to MongoDB at 127.0.0.1

connect to 'machinesdb'

connect to robotsCollection

$result = robotsCollection::find()

print_r($results)

edited Sep '17

You'd have to use Phalcon incubator ATM, as there's no full support for new MongoDB extension in official PhalconPHP yet :/



2.5k
Accepted
answer

For anyone still struggling to get with php 7 and phalcon 3.22 working .... in a test enviroment Create this file and save as index.php (it only serves to prove that everything is setup properly for a new installation.

using composer... for ubuntu users run from commmand line

composer require phalcon/incubator:^3.2

check if "mongoDB" is loaded properly using phpinfo(). If it is not loaded code below will show

Error Class "MongoDB\Driver\Manager" not found'

<?php
//ensure errors are displayed
ini_set('display_errors', 1);
error_reporting(E_ALL ^ E_NOTICE);

// phpinfo(); // uncomment to check 

include_once('vendor/autoload.php');

$mongoClient = new \Phalcon\Db\Adapter\MongoDB\Client("mongodb://127.0.0.1");
$mongodb = $mongoClient->selectDatabase("test");

echo "<pre>";
print_r($mongodb);

if it all works properly and phalconphp connects to MongoDB it will simply output the connection object which includes the database name it is connected to