MongoDB problem in data listing with php

0

var mongoose          = require('mongoose');
var db                = mongoose.connection;

// db connect
db.on('error', console.error);
db.once('open', function() {
  console.log('Conectado ao MongoDB.')
});
mongoose.connect('mongodb://localhost/rotiny');
// -- //

var msnSchemaUser = new mongoose.Schema({
  name: String,
  username: String,
  password: String,
  email: String,
  roles: {
    type: Array,
    default: ['user']
  }
});

var msnModuleUser = mongoose.model('User', msnSchemaUser);

var query = msnModuleUser.find();
query.exec('find', function(err, items) {
    console.log(items)
});
  

Results that the code returns.

[ { roles: [ 'user' ],
    __v: 0,
    email: '[email protected]',
    password: '12345',
    username: 'rotiny',
    name: 'Rotiny - BETA API',
    _id: 5685e3f16d3e65ba3fa47e84 },
  { roles: [ 'user' ], __v: 0, _id: 5685e42f8deb044f405c6986 },
  { roles: [ 'user' ], __v: 0, _id: 5685e4e38deb044f405c6987 },
  { roles: [ 'user' ], __v: 0, _id: 5686068a8deb044f405c6988 },
  { roles: [ 'user' ], __v: 0, _id: 5686068a8deb044f405c6989 },
  { roles: [ 'user' ], __v: 0, _id: 5686068f8deb044f405c698a },
  { roles: [ 'user' ], __v: 0, _id: 568606908deb044f405c698b },
  { roles: [ 'user' ], __v: 0, _id: 5686069b8deb044f405c698c } ]
  

My question is the following.

Because I can not list those same data with this code in PHP

$m = new Mongo("mongodb://localhost"); // connect
$db = $m->selectDB("rotiny");

$collection = new MongoCollection($db, 'User');

$find = $collection->find();

foreach ($find as $doc) {
    var_dump($doc);
}

The code just above in php, does not return anything.

  

Note: I have recently been using Mongo.

Can anyone tell me what I'm doing wrong?

    
asked by anonymous 07.01.2016 / 15:39

2 answers

0

The mongo driver returns a cursor and not a PHP array, you will not debug your query with a var_dump as you are doing in your PHP example. In case your example with mongoose for the node works the same logic because the mongoose is an Object Document Model (ODM) that takes care of using the functions of the mongo driver for javascript that serves to manipulate the cursor, and then it does the conversion of the results to an object giving you the possibility to manipulate the return directly as object, the Mongo driver for PHP does not do this in all its methods.

Ideally, you should consult the PHP driver documentation which methods return an array to you, and which ones return a cursor, for the examples you will know how to manipulate the returns according to the query method you are using.

PHP driver's official reference: link

    
26.10.2016 / 19:44
0

Try using the following function to establish connection

    $m = new Mongo("mongodb://DATABASE_USERNAME:DATABASE_PASSWORD@HOST");
    $db = $m->selectDB($database); // Connect to Database

If you do not use a password:

    $m = new Mongo("mongodb://DATABASE_USERNAME@HOST");
    $db = $m->selectDB($database); // Connect to Database

For example

    $m = new Mongo("mongodb://root@localhost");
    $db = $m->selectDB($database); // Connect to Database

Note that the use of class Mongo has been deprecated and the use of MongoDB is recommended. See more at link

    
07.01.2016 / 15:50