假设这样的设置:
blogposts
{
title:"Example",
slug:"example-post"
tags: ["foo", "bar"]
},
{
title:"Example2",
slug:"example2"
tags: ["foo"]
}
news
{
headline: "Test"
slug: "test-news"
tags: ["bar"]
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以获得所有带有特定标签的博文:
$cursor = $blogposts->find(array('tags' => 'bar'));
Run Code Online (Sandbox Code Playgroud)
但有没有办法一次查询多个集合,以获得所有带标签的文件?例如,显示标签为"bar"的所有内容.
Nie*_*est 13
无法一次查询多个集合.
如果文档都是相同的常规类型,最好的方法是将所有文档存储在同一个集合中.在您的示例中,博客帖子和新闻项目都是一种"内容".
content
{
type: "blogpost",
title: "Example",
slug: "example-post"
tags: ["foo", "bar"]
},
{
type: "blogpost",
title: "Example2",
slug: "example2"
tags: ["foo"]
},
{
type: "news",
headline: "Test"
slug: "test-news"
tags: ["bar"]
}
Run Code Online (Sandbox Code Playgroud)
这种方法利用了MongoDB的无模式特性; 虽然两种文档类型可能具有不同的属性,但它们都可以存储在同一个集合中.这允许您查询所有内容,或仅查询某些类型的内容,具体取决于您的要求.
| 归档时间: |
|
| 查看次数: |
4137 次 |
| 最近记录: |