Home > Back-end >  how to get the existing collection in mongodb using mongose- Nodejs
how to get the existing collection in mongodb using mongose- Nodejs

Time:05-18

In my mongodb i already had a collection and document enter image description here

Now, i want to use this collection in my node-js using mongoose. how we do this.

const mongoose = require("mongoose");
const schema = mongoose.Schema;
const adminLogin = new schema({
name: { type: String, required: true },
password: { type: String, required: true }
})
module.exports = mongoose.model("adminDetails", adminLogin)

while doing this it is creating new collection. Unable to use the existing collection.

CodePudding user response:

  1. At first you need to make a GET method Router in your jsFile.

like this

app.get("/mainData", async (req, res) => {
  const menuInfo = await Nutrients.find({});
  res.json(menuInfo);
});
  1. You can set and use VSCode extension "thunderClient"!

like this

Before Execution of above code

In this above picture the structure of dB, one collection having 5 collections. I'm trying to use adminDetails collection. But, after running the above code it is creating the new collection. But, i'm unable to use the existing collection. FYI: After Execution of above code

  • Related