Unable to to use to my mongodb Atlas database

Viewed 31

I am using MongoDB Atlas as my database. when I search http://localhost:5000/book.
I got a error like this - MongoServerError: user is not allowed to do action [find] on [product.book]. did I search wrong? It should be book or books.

const express = require('express');
const cors = require('cors');
require('dotenv').config();
const port = process.env.PORT || 5000;
const app = express();
const { MongoClient, ServerApiVersion } = require('mongodb');

// midlewarwe

app.use(cors());
app.use(express.json());

const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.ibawcfn.mongodb.net/?retryWrites=true&w=majority`;
const client = new MongoClient(uri, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  serverApi: ServerApiVersion.v1,
});

async function run() {
  try {
    await client.connect();
    const bookCollection = client.db('product').collection('book');

    app.get('/book', async (req, res) => {
      const query = {};
      const cursor = bookCollection.find(query);
      const books = await cursor.toArray();
      res.send(books);
    });
    // app.post('/books', async (req, res) => {

    //     const  newBook = req.body;
    //     const result = await bookCollection.insertOne(newBook);
    //     res.send(result);
    // });
  } finally {
  }
}
run().catch(console.dir);

app.get('/', (req, res) => {
  res.send('hello world ');
});

app.listen(port, () => {
  console.log('listening on port ', port);
});
1 Answers

It is likely caused by database access to your MongoDB Atlas. Your DB_USER must have at least Only read any database role to perform read operation or Read and write to any database role to perform both read and write.

  1. Go to your MongoDB Atlas console
  2. On the left hand menu, select Database Access
  3. Identify the username you use for your DB_USER
  4. Find that username and select EDIT in Actions column
  5. Find Built-in Role menu in Database User Privileges section
  6. Expand the menu and choose either Only read or Read and write role
  7. Finally, click Update User button to apply change.
Related