mongodb 中做 join 的方法

【问题】

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

imagepng

A3:employee

imagepng

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

imagepng