Create view from multiple database

439 Views Asked by At

I learned about using db.siblingsDB and db.sisterDB. But I can't use both in creating a view from different databases.

My example query I tried are:

use "database1"
var users = db.getSiblingDB("database2").getCollection("User").find({})
    db.runCommand({
        "create" : "VW_User",
        "viewOn" : users,
        "pipeline" : [ 
            {
                "$lookup" : {
                    "localField" : "loginName",
                    "foreignField" : "userList.loginName",
                    "from" : "Site",
                    "as" : "User&Site"
                }
            }, 
            {
                "$unwind" : "$User&Site"
            }, 
            {
                "$unwind" : "$User&Site.userList"
            },
            {
                "$project" : {
                    "_id" : 0,
                    "userId" : "$_id",
                    "userRole" : "$User&Site.userList.role",
                    "siteId" : "$User&Site.id",
                    "active" : "$active",
                    "connectedAtTime" : "$connectedAtTime",
                    "lastMessageTime" : "$lastMessageTime"
                }
            }
        ]
    })

But this is not working. Someone please tell me whether it is possible or not.

0

There are 0 best solutions below