MongoDB PHP嵌套文档

ok,假设我的电视数据库中有这个集合</ p>

 “season”:“1”,“剧集”:{“code”:[“1x01”,“1x02”,“1x03”],“title”:[“Dont Look Back”,“Genesis”,“Third”  ]},
“season”:“2”,“剧集”:{“code”:[“2x01”,“2x02”,“2x03”],“title”:[“D2ont Look Back”,“G2enesis” ,“T2hird”]},
“季节”:“3”,“剧集”:{“代码”:[“3x01”,“3x02”,“3x03”],“标题”:[“D3ont回头看” ,“G3enesis”,“T3hird”]},
“season”:“4”,“剧集”:{“code”:[“4x01”,“4x02”,“4x03”],“title”:[“ D4ont回顾“,”G4enesis“,”T4hird“]}
</ code> </ pre>

我如何制作它只是,让我们说第2季的剧集?

ive一直在尝试使用

echo $ obj ['episodes'] ['code'] [0]; </ code>

仅显示最后一行的剧集</ p >

我非常确定我的嵌套是完全错误的,但我是mongo的新手,我很难尝试将其映射出来</ p>

任何建议?</ p>
</ DIV>

展开原文

原文

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个回答

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

du31992
du31992 我明白了,谢谢!
接近 10 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐