這裏是它沒有嵌入文檔的代碼。Mongodb不用php獲取嵌入文檔
<?php
include 'vendor/dbcon.php';
$documentlist = $state -> find(['s_name' => 'West Bengal'],
['projection' => ['_id' => 0, 'places._id' => 1]]);
foreach ($documentlist as $doc) {
echo $doc["places._id"] . "\n";
}
?>
ERROR shows: Notice: Undefined index: places._id in C:\xampp\htdocs\Education\Home.php on line 130
如果代碼是這樣的:
foreach ($documentlist as $doc) {
echo $doc["_id"] . "\n";
}
then ERROR:: Notice: Undefined index: _id in C:\xampp\htdocs\Education\Home.php on line 130
數據庫結構:MongoDB的
db.state.find().pretty()
{
"_id" : "state1",
"s_name" : "West Bengal",
"places" : [
{
"_id" : "Bardhaman",
"pin" : "754216"
},
{
"_id" : "Jalpaiguri",
"pin" : "754217"
}
]
}
請幫助我,我怎麼能打印所有places._id列表(來自Bardhaman,Jalpaiguri)
您不包括'_id'。看到? '_id'=> 0' –
我的不好。 「地方」當然是一個陣列。您需要在文檔中對其進行迭代。 –