Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP代码从mongodb中名为group的集合中获取数据_Php_Mongodb_Drupal 7 - Fatal编程技术网

如何使用PHP代码从mongodb中名为group的集合中获取数据

如何使用PHP代码从mongodb中名为group的集合中获取数据,php,mongodb,drupal-7,Php,Mongodb,Drupal 7,这是我从mongodb中的集合获取数据的PHP代码: $m = new Mongo('localhost'); $m->connect(); $db = $m->mydb; $collection = $db->fields_current; $cursor = $collection->find(); foreach ($cursor as $obj) { echo $obj["title"] . "\n"; } 该代码适用于当前的集合字段。但是,我有这样一个

这是我从mongodb中的集合获取数据的PHP代码:

$m = new Mongo('localhost');
$m->connect();
$db = $m->mydb;
$collection = $db->fields_current;
$cursor = $collection->find();
foreach ($cursor as $obj) {
    echo $obj["title"] . "\n";
}
该代码适用于当前的集合
字段
。但是,我有这样一个集合
fields\u current.node
,其中
node
是集合的命名组。如何从命名组获取数据

编辑:

按照PeterM的回答,我得到了一个空的MongoCursor对象。这是我的新代码:

$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";
}
更多细节:我正在使用Drupal,
node
指的是“node”表,
type
指的是“node type”。集合名称为
fields\u current.node
,记录结构如下:

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',
  ),
)

有关更多示例,请参见

,因为它的Drupal不需要指定数据库名称,就像编写MySql查询时一样。还有一个不同的函数用于获取集合对象

// 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...
  ....
}

但它返回一个空的MongoCursor对象。如何从那里取回田地。我查阅了文档,看起来我遗漏了什么。我是mongodb新手。查询是要查找的第一个参数,而字段是第二个参数。你做了一个数组。它应该是
$collection->find($query,$fields)所以它应该是
$query=array('group_name'=>'节点','type'=>'内容类型')(注意:您没有字段组\u名称,是否应该是\u类型?)和
$fields=array('title')
// 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...
  ....
}