Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda