【发布时间】:2017-04-02 12:10:04
【问题描述】:
我有一个类似这样的 mongodb 文档:
{
"id": 1,
"title": "This is the title",
"body" : "This is the body",
"comments": [
{
"email_address": "mirko.benedetti@somemail.com",
"name": "Mirko",
"surname": "Benedetti",
"language": "it",
"text": "This is a message",
"published": "Y",
"on": "2014-03-22 15:04:04"
},
{
"email_address": "marc.surname@somemail.com",
"name": "Marc",
"surname": "Surname",
"language": "it",
"text": "Another Message",
"published": "N",
"on": "2014-03-23 15:04:05"
}
]
}
我有一个这样的查询:
$this->db->collection->find(array('id' => $id, 'language' => $lang, 'comments.published' => 'Y'),
array('comments.name' => 1, 'comments.surname' => 1, 'comments.text' => 1, 'comments.on' => 1, '_id' => 0));
我的问题是运行该查询时,mongodb 返回两个 cmets,这是我不想要的,我只想要带有“已发布”的消息:“Y”。
例如,我尝试运行 'cmets.published' => 'something' 并没有选择任何评论,这是正确的,但如果至少有一个 cmets 有 将“已发布”标志设置为“Y”,同时显示两个 cmets。
欢迎任何帮助。
标签: php mongodb nested-documents