Answers for "How to access a preexisting collection with Mongoose?"

0

How to access a preexisting collection with Mongoose?

const mongoose = require("mongoose");
mongoose.connect('mongodb://localhost/productsDB');

const connection = mongoose.connection;

connection.on('error', console.error.bind(console, 'connection error:'));
connection.once('open', async function () {

  const collection  = connection.db.collection("Products");
  collection.find({}).toArray(function(err, data){
      console.log(data); // it will print your collection data
  });

});
Posted by: Guest on March-29-2022

Code answers related to "How to access a preexisting collection with Mongoose?"

Code answers related to "Javascript"

Browse Popular Code Answers by Language