1

i've 2 collections, named user-profile from database user-service and user-wishlist from database pcrf-service.

is it possible to do $lookup function in mongodb from another database?

i've used this: db.getSiblingDB('dbname').collection.command()

but it's not working.

here's my code:

db = db.getSiblingDB("user-service");
db.getCollection("user-profile").aggregate(
    [
        { 
            "$sort" : { 
                "createdOn" : -1.0
            }
        }, 
        { 
            "$lookup" : { 
                "from" : "pcrf-service.user-wishlist", 
                "localField" : "mobile", 
                "foreignField" : "userId", 
                "as" : "contents"
            }
        }
    ], 
    { 
        "allowDiskUse" : false
    }
);
  • Does this answer your question? [Is it possible to do a $lookup aggregation between two databases in Mongodb?](https://stackoverflow.com/questions/39222798/is-it-possible-to-do-a-lookup-aggregation-between-two-databases-in-mongodb) – turivishal Apr 30 '21 at 17:45
  • nope, i''ve tried – yunan helmi Apr 30 '21 at 17:47
  • i don't know what have you tried, but the answer in that question says its not possible to lookup in another database's collection. – turivishal Apr 30 '21 at 18:07
  • From the documentation: "$lookup New in version 3.2. Performs a left outer join to an unsharded collection in the same database" – Joe May 01 '21 at 04:22

0 Answers0