gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:59:43 24 4
gpt4 key购买 nike

这是我从 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_current。但是,我有一个像这样的集合 fields_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 指的是“节点”表,type 是“节点类型” .集合名称是 fields_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',
),
)

最佳答案

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

参见 http://www.php.net/manual/en/mongo.sqltomongo.php更多例子

关于php - 如何使用 PHP 代码从 mongodb 中名为组的集合中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11456267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com