2

This is my PHP code for fetching data from a collection in mongodb:

$m = new Mongo('localhost');
$m->connect();
$db = $m->mydb;
$collection = $db->fields_current;
$cursor = $collection->find();
foreach ($cursor as $obj) {
    echo $obj["title"] . "\n";
}

The code works fine for the collection fields_current. But, I have a collection like this fields_current.node where node is the named group of the collection. How do I fetch data from the named group.

Edit:

After following PeterM's answer I am getting an empty MongoCursor object. This is my new code:

$m = new Mongo('localhost');
$m->connect();
$db = $m->mydb;
$collection = $db->fields_current;
$query = array(array('group_name' => 'node', 'type' => 'content-type'), array('title' => 1)); // SELECT title FROM fields_current WHERE group_name = 'node' AND type = 'content-type'
$cursor = $collection->find($query);
foreach ($cursor as $obj) {
    echo $obj["title"] . "\n";
}

Some more details: I am using Drupal and node refers to the 'node' table, type is the 'node-type'. The collection name is fields_current.node and the record structure is this:

array (
  '_id' => new MongoInt32(37),
  '_type' => 'node',
  '_bundle' => 'content-type',
  '_revision_id' => new MongoInt32(37),
  'nid' => new MongoInt32(37),
  'vid' => new MongoInt32(37),
  'type' => 'content-type',
  'language' => 'und',
  'title' => 'title',
  'uid' => new MongoInt32(1),
  'status' => new MongoInt32(1),
  'created' => new MongoInt32(1342065549),
  'changed' => new MongoInt32(1342065549),
  'comment' => new MongoInt32(1),
  'promote' => new MongoInt32(0),
  'sticky' => new MongoInt32(0),
  'tnid' => new MongoInt32(0),
  'translate' => new MongoInt32(0),
  'field_cutom_field' => 
  array (
    'value' => 'foobar',
  ),
)

2 Answers 2

3
$cursor = $collection->find(array('node' => 'group_name'));

See http://www.php.net/manual/en/mongo.sqltomongo.php for more examples

2
  • But it returns an empty MongoCursor object. How to fetch the fields from there. I referred the documentation, looks like I am missing something. I am new to mongodb. Jul 13, 2012 at 5:28
  • Query is first parameter to find, while fields is second. You made one array. It should be $collection->find($query, $fields); so it should be $query = array('group_name' => 'node', 'type' => 'content-type'); (NOTE: you don't have field group_name, should it be _type?) and $fields = array('title')
    – user133408
    Jul 13, 2012 at 8:39
2

Since its Drupal you do not have to specify the database name, just like when you write queries for MySql. And also there is a different function for getting a collection object.

// Suppose you have a collection called 'collection_name', then you can get its object using this code.
$collection = mongodb_collection('collection_name');

// Suppose you have a collection called 'collection_name.group', where 'group' is the named group of this collection, then you can get its object using this code.
$collection = mongodb_collection('collection_name', 'group');

// Rest of the operations will be same.
$cursor = $collection->find(array('type' => 'content-type'));
foreach ($cursor as $obj) {
  ....
  // Do your work...
  ....
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.