Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
Mongodb database.collection confusion

I created a MEAN project in heroku.
In MongoDB I have a DB / Collection like this:
db == content / collection == android_main
I have verified that the collection is in the proper database (and not in admin) via the following MongoSH exchange:

Atlas atlas-xxxxxx-shard-0 [primary] content> use admin
switched to db admin
Atlas atlas-xxxxxx-shard-0 [primary] admin> show collections

Atlas atlas-xxxxxx-shard-0 [primary] admin> use content
switched to db content
Atlas atlas-xxxxxx-shard-0 [primary] content> show collections
android_main

In MongoDB I also have a user w/ specific privs to read this db/collection.
enter image description here

In the code (Node.js) I use the following connection string:
mongodb+srv://<USER>:<PASS>@<MONGO URL>/content?retryWrites=true&w=majority
The connection completes successfully. I pass in the user I mentioned above, e.g. the user with just the specific read privs on the database (content) and collection (android_main).
In the code, the mechanism I use to get the database variable via the connection and connection string specifically is:

mongodb.MongoClient.connect(process.env.MONGODB_URI, function (err, database) {

Now, in the code, in response to an `HTTP GET` I issue the following:
db.collection("android_main").find({}).toArray(function(err, docs) {
...
}

I get back this error:
ERROR: user is not allowed to do action [find] on [admin.android_main]

Question: how can I modify my code a/o setup to ensure the db.collection.find() call references the proper database? I would have thought this was taken care of in the connection, where the database is explicitly called out.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I'm going to post the following answer which solves the issue above. I found it by just playing around with random ideas. I'd like to thank Heroku and their pathetic documentation for turning a 30 minute task into a 3 day ordeal.

client.connect(
    process.env.MONGODB_URI,
    function (err, database) {
        console.log("CONNECT...");
        if (err) {
            console.log(err);
            process.exit(1);
        }
        db = database.db(DATABASE);
        var server = app.listen(process.env.PORT || 8080,
            function () {
                var port = server.address().port;
                console.log("CONNECT App now running on port", port);
            }
        );
        console.log("CONNECT Done.");
    }
);

Apparently, to properly set the variable db (the one which you will use in the queries later) you should note that the database you receive in the connection callback is the admin database (that which was used for auth), and from that you make the db() request passing in the database you intend to use. All of this is utterly redundant b/c we pass the database we intend to use in the connection string. What a complete CF; but it's certainly not the 1st one.
Enjoy.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda