Mongodb problem in listing data with php

Asked

Viewed 327 times

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 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 doubt and the following.

Because I can’t list, the 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, returns nothing.

Note: I’ve been using Mongo for a while.

Can someone tell me what I’m doing wrong?

2 answers

0

The driver Mongo 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 the case of your example with Mongoose for Node works the same logic because Mongoose is an ODM (Object Document Model) who takes care of using the functions of the Javascript driver that serve to manipulate the cursor, and then he converts the results to an object giving you the possibility to manipulate the return directly as an object, Mongo driver for PHP does not do this in all its methods.

Ideally, you can find in the PHP driver documentation which methods return an array, and which return a cursor, for example you will know how to manipulate the returns according to the query method you are using.

Official PHP driver reference: https://docs.mongodb.com/ecosystem/drivers/php/#drivers

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 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

It is noteworthy that the use of class Mongo has been discontinued and the use of MongoDB is recommended. See more on http://php.net/manual/en/mongodb-driver-manager.construct.php

  • I don’t use password in Mongo.

  • Use without the :DATABASE_PASSWORD in that case.

  • Sorry I was not specific, I do not use user and password in Mongo.

  • I’m seeing if changing the class resolves, thanks for your attention.

  • Because and friend, I can’t get the data recorded with Mongoose in php. you at least helped me realize this, thank you very friend! to solve my problem, I had to use the https://github.com/mongodb/node-mongodb-nativemodule.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.