gpt4 book ai didi

javascript - PHP/MongoDB 查询协助

转载 作者:行者123 更新时间:2023-12-03 07:48:42 24 4
gpt4 key购买 nike

我有一个包含多个文档的 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,但只返回“snaps”记录及其值

当前代码:

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());
}

最佳答案

以下命令将返回集合中具有非空“snaps”记录及其值的所有对象:

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

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

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

希望对你有帮助!

关于javascript - PHP/MongoDB 查询协助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35097171/

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