How to Connect/Query Mongoose to an existing Database How to Connect/Query Mongoose to an existing Database mongoose mongoose

How to Connect/Query Mongoose to an existing Database


You can refer to the answer given below, just pass an empty object in schemalike db.model('users', new Schema({}))


Create an empty schema for each collection you want to useand then create a model to be used in your projectthe model take 3 parameter1)name of the model2)schema name3)collection name ( from mongodb atlas)

like that

const mongoose = require('mongoose');mongoose.connect('mongodb uri')const userSchema = new mongoose.Schema({});const User = mongoose.model('User', userSchema, 'user');

then you can use the model normally

User.find({})


connection to mongo db

// Connect to mongoDBmongoose.connect('mongodb://localhost/[yourDbName]',{useNewUrlParser:true})    .then(function(){        console.log('mongoDB connected');    })    .catch(function(){        console.log('Error :');    }) 

after that you will have to create your schema and then only you can query the database

create your schema like this

// Crimes Schema const CrimeDetailsSchema= new Schema({    first_name: {        type: String,        required: true    },    last_name: {        type: String,        required: true    },    email: {        type: String,        required: true    }});const Profile = module.exports = mongoose.model('delitosCollection', CrimeDetailsSchema, 'delitosCollection');

after that create your queries

you can get an idea about that in mongoose documentation here