PHP / MongoDB查询帮助(PHP/MongoDB Query Assistance)

系统教程 行业动态 更新时间:2024-06-14 17:02:17
PHP / MongoDB查询帮助(PHP/MongoDB Query Assistance)

我有一个包含多个文档的mongodb集合。 我想搜索所有文档,并返回'foo'记录的每个文档实例

示例集合:

Object 1: [_id] => MongoId Object ( [$id] => 551993579285313235ebd120 ) [snaps] => Array ( [0] => ["84062","3","","0-250000","1"] [1] => ["84062","4","","0-350000","1"] ) [zip] => 84057 Object 2: [_id] => MongoId Object ( [$id] => 55198dfc928531ea36ebd11f ) [zip] => testz Object 3: [_id] => MongoId Object ( [$id] => 56a1594e9285319a0a22e15c ) [snaps] => Array ( [0] => ["84057","3","","0-250000","1"] [1] => ["84020","4","","0-350000","1"] ) [zip] => 84062

我想返回对象1和3,但只返回“快照”记录及其值

当前代码:

try { $conn = new Mongo('localhost'); $db = $conn->remo_db1; $c = $db->eudata; $cursor = $c->find(array('snaps')); foreach($cursor as $obj) { echo $obj['snaps']; } // disconnect from server $conn->close(); } catch (MongoConnectionException $e) { die('Error connecting to MongoDB server'); } catch (MongoException $e) { die('Error: ' . $e->getMessage()); }

I have a mongodb collection with multiple documents. I want to search all the documents, and return each documents instance of a record for 'foo'

Example Collection:

Object 1: [_id] => MongoId Object ( [$id] => 551993579285313235ebd120 ) [snaps] => Array ( [0] => ["84062","3","","0-250000","1"] [1] => ["84062","4","","0-350000","1"] ) [zip] => 84057 Object 2: [_id] => MongoId Object ( [$id] => 55198dfc928531ea36ebd11f ) [zip] => testz Object 3: [_id] => MongoId Object ( [$id] => 56a1594e9285319a0a22e15c ) [snaps] => Array ( [0] => ["84057","3","","0-250000","1"] [1] => ["84020","4","","0-350000","1"] ) [zip] => 84062

I want to return object 1, and 3, but only the "snaps" records, and their values

Current code:

try { $conn = new Mongo('localhost'); $db = $conn->remo_db1; $c = $db->eudata; $cursor = $c->find(array('snaps')); foreach($cursor as $obj) { echo $obj['snaps']; } // disconnect from server $conn->close(); } catch (MongoConnectionException $e) { die('Error connecting to MongoDB server'); } catch (MongoException $e) { die('Error: ' . $e->getMessage()); }

最满意答案

以下内容将返回集合中具有非空“快照”记录及其值的所有对象:

$ cursor = $ c-> find(array('snaps'=> array('$ nin'=> array('','',null))),array('snaps'=> true));

第一个参数是查询。 它返回集合中包含非空“捕捉”值的所有对象。

第二个参数使得只返回'snaps'字段。 (如果要返回所有字段,只需删除第二个参数)。

希望能帮助到你!

The following will return all objects in the collection with non-empty 'snaps' records and their values:

$cursor = $c->find( array('snaps'=>array('$nin' => array(' ','',null))), array('snaps'=>true));

The first parameter is the query. It returns all objects in the collection that contain non-empty 'snaps' values.

The second parameter makes it so only the 'snaps' field gets returned. (If you want to return all the fields, simply remove the second parameter).

Hope it helps!

更多推荐

本文发布于:2023-04-21 18:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/2e0b626c00c7349e9d405a9f1ba46a50.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MongoDB   PHP   Assistance   Query

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!