【问题】
Imagine you have a collection for posts, and each of these posts has the attribute userid: ObjectId( ), where ObjectID is referencing a document in the Users collection.
How would you go about retrieving the user information (in this case, the user document might contain a username, email, full name, etc) as part of a query on the Posts collection?
【回答】
用 Mongodb 是不支持 join 查询的, 要多次查询很不方便。可以考虑配合 mongodb 使用的本身有计算能力的工具,比如使用 SPL。举例说明:
通过 employee 文档和 seller 文档查询 employee.state 为 California 的 sales orders。
A | |
1 | =mongo_open(“mongo://localhost:27017/local?user=test&password=test”) |
2 | >sales=mongo_shell(A1,”test38.find()”) |
3 | >employee=mongo_shell(A1,”test39.find()”).switch(SELLERID,sales:EID) |
4 | =A3.select(SELLERID.STATE==“California”) |
5 | >mongo_close(A1) |
A2:sales

A3:employee

A4:选出满足条件的结果,订单 1、3 都属于来自 California 的雇员 1

在MongoDB中,官方并不支持JOIN查询。针对这种情况,可以利用MongoDB的数据计算工具,如SPL,来实现类似的功能。例如,查询employee.state为California的sales orders,需要分别从employee和seller文档中获取数据,然后通过SPL进行处理,得到满足条件的结果。这种方法虽然比直接JOIN查询复杂,但能完成所需的数据整合。
74

被折叠的 条评论
为什么被折叠?



