Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!