Not Authorized to run lookup

Im trying to run the example lookup in chapter and run ito thios error

MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.air_alliances.aggregate(pipeline).pretty()
2020-02-09T22:46:58.732-0300 E QUERY [js] Error: command failed: {
“operationTime” : Timestamp(1581299217, 1),
“ok” : 0,
“errmsg” : "not authorized on admin to execute command { aggregate: “air_alliances”, pipeline: [ { $lookup: { from: “air_airlines”, local_field: “airlines”, foreignField: “name”, as: “airlines” } } ]

I relogged into mongoshell and was able to run the query
Im not sure which collection I am logged in, but it works from this one.
I had another error, local_field instead of localField, its working now

my pipeline is
var pipeline = [
{
$lookup: {
from: “air_airlines”,
local_field: “airlines”,
foreignField: “name”,
as:“airlines”
}
}
]

same as the example in chapter 3 of 121. Any tips?

I have tried using several collections as starting points ( use air_alliances, use admin, use aggregations… )

Thanks

Unfortunately, I don’t understand your post. Is there still a problem?

Hi.

No, the relog solved it.Is there a way to mark it as solved?
But I dont understand why the database I am using matters when running a lookup, or how to determine which was the appropriate collection to do it in since using another collection caused an error.

Thanks for the help.

Under every post, there’s a Solved button or checkbox.

In this course, the only db you have access to is the aggregations db. You were using the wrong database, in this case the admin db hence the error. But in the connection strings, the default database is the aggregations db which is why it worked after you logged in again. If you had switched to the aggregations db (i.e. use aggregations), you definitely would not have received a “not authorized” error.

To check the name of the current db, just type db.

The name of the collection is whatever you type in the query, i.e. db.collection.aggregate(), and in $lookup it’s the from field.

1 Like

Thank you,
I do believe I tried a couple of databases, the error started after I used air_alliances, but maybe it was another error that I misread after I went to aggregations.

But the db..aggregate() has the intended collection, why does it make a difference which database you are currently using? Isn’t it all in memory or local disk?
Does it create temp results in that database?

I don’t know if you come from a SQL world or not, but if you do, the principles of databases in SQL is similar to that of MongoDB’s NoSQL databases. I.e. a table/collection is stored in a specific database so in order to reference that collection, you must switch to the database where the collection is stored. And with $lookup the collections must be within the same database.