MongoDB一次查询多个集合 [英] MongoDB query multiple collections at once

查看:1746
本文介绍了MongoDB一次查询多个集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

users
{
 "_id":"12345",
 "admin":1
},
{
 "_id":"123456789",
 "admin":0
}

posts
{
 "content":"Some content",
 "owner_id":"12345",
 "via":"facebook"
},
{
 "content":"Some other content",
 "owner_id":"123456789",
 "via":"facebook"
}

$ b b

这是我的mongodb的一个示例。我想得到所有的via属性等于facebook并由管理员(admin:1)发布的帖子。我不知道如何获取这个查询。因为mongodb不是关系数据库,我不能做连接操作。可能的解决方案?

Here is a sample from my mongodb. I want to get all the posts which has "via" attribute equal to "facebook" and posted by an admin ("admin":1). I couldn't figure out how to acquire this query. Since mongodb is not a relational database, I couldn't do a join operation. What could be the solution ?

推荐答案

尝试在MongoDB中JOIN会破坏使用MongoDB的目的。但是,您可以使用 DBref 并编写您的应用程序级代码(或库),以便自动为您提取这些引用。

Trying to JOIN in MongoDB would defeat the purpose of using MongoDB. You could, however, use a DBref and write your application-level code (or library) so that it automatically fetches these references for you.

或者您可以更改您的模式并使用嵌入文档

Or you could alter your schema and use embedded documents.

您最后的选择是将事情保持原样,做两个查询。

Your final choice is to leave things exactly the way they are now and do two queries.

这篇关于MongoDB一次查询多个集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆