$lookup MongoDB cross database query

2,108 views
Skip to first unread message

dahaol hao

unread,
Jun 21, 2017, 6:01:45 PM6/21/17
to mongodb-user

Is it possible to do a $lookup aggregation between two databases in Mongodb?


For example:
I have a collection, called "bookshelves" in a database(just call it aaa). But have another collection, called "picture_books" in another database (just call it bbb).

I want to do like this:
db.bookshelves.aggregate([{$match: {"child_id":262}},{$lookup: {from: "picture_books", localField: "bookshelfable_id", foreignField: "book_id", as: "picture_books"}}]).pretty()

it doesn't work. 

Mongodb says
"Performs a left outer join to an unsharded collection in the same database to filter in documents from the “joined” collection for processing."

so, How can i solve it?



Asya Kamsky

unread,
Jun 24, 2017, 11:51:07 PM6/24/17
to mongodb-user
Make sure both collections are in the same database.  Cross-database $lookup is not allowed.


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/62b8f314-46ac-40ae-be77-57ca5247d2de%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Asya Kamsky
Lead Product Manager
MongoDB
Download MongoDB - mongodb.org/downloads
We're Hiring! - https://www.mongodb.com/careers
Reply all
Reply to author
Forward
0 new messages