douling8772 2010-12-03 19:38
浏览 44
已采纳

MongoDB PHP嵌套文档

ok lets say i have this collection in my tv database

"season" : "1", "episodes" : {"code" : ["1x01", "1x02", "1x03"], "title" : ["Dont Look Back", "Genesis", "Third"]},
"season" : "2", "episodes" : {"code" : ["2x01", "2x02", "2x03"], "title" : ["D2ont Look Back", "G2enesis", "T2hird"]},
"season" : "3", "episodes" : {"code" : ["3x01", "3x02", "3x03"], "title" : ["D3ont Look Back", "G3enesis", "T3hird"]},
"season" : "4", "episodes" : {"code" : ["4x01", "4x02", "4x03"], "title" : ["D4ont Look Back", "G4enesis", "T4hird"]}

how do i make it so that only, lets say the episodes from season 2 are shown? ive been trying using
echo $obj['episodes']['code'][0];
but it only shows episodes from the last row

im pretty sure my nesting is all wrong but im new to mongo and im having trouble trying to map this out

any advice?

  • 写回答

1条回答 默认 最新

  • douzhai7873 2010-12-03 21:09
    关注

    You need to use find() first. $a = $coll->findOne(array('season' => '2'); That will return an array $a which will have keys 'episodes', 'title' So once you find the document with findOne, you would then just access the data using $a['title'] or whatever fields you need

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥20 jupyter保存图像功能的实现
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键