title: set database and collection using mongoose
body: <p>How do I set my database and collection using mongoose ? I am trying to connect to a mongodb atlas database using mongoose. My database is called "test_db" and collection name is "users" where would I specify that information ?</p>
  
  <p>This is my shema (data.js):</p>
  
  <pre><code>// /backend/data.js
  const mongoose = require("mongoose");
  const Schema = mongoose.Schema;
  
  // this will be our data base's data structure 
  const DataSchema = new Schema(
    {
      _id: Number,
      name: String,
      password: String
    }
  );
  
  // export the new Schema so we could modify it using Node.js
  module.exports = mongoose.model("users", DataSchema);
  </code></pre>
  
  <p>and this is server.js:</p>
  
  <pre><code>const mongoose = require('mongoose');
  const express = require('express');
  var cors = require('cors');
  const bodyParser = require('body-parser');
  const logger = require('morgan');
  const Data = require('./data');
  
  const API_PORT = 3001;
  const app = express();
  app.use(cors());
  const router = express.Router();
  
  // this is our MongoDB database
  const dbRoute = 'mongodb+srv://&lt;user&gt;:&lt;password&gt;@cluster0-bmihj.mongodb.net/test?retryWrites=true&amp;w=majority';
    ;
  
  // connects our back end code with the database
  const conn = mongoose.connect(dbRoute, { useNewUrlParser: true });
  
  //let db = mongoose.connection;
  const db = conn.db('test_db');
  
  var MyModel = mongoose.model('Test', new Schema({ name: String }));
  
  db.once('open', () =&gt; console.log('connected to the database'));
  
  // checks if connection with the database is successful
  db.on('error', console.error.bind(console, 'MongoDB connection error:'));
  
  
  // this is our get method
  // this method fetches all available data in our database
  router.get('/getData', (req, res) =&gt; {
    Data.find((err, data) =&gt; {
      if (err) return res.json({ success: false, error: err });
      return res.json({ success: true, data: data });
    });
  });
  </code></pre>