Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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
  });

});
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #How #access #preexisting #collection
ADD COMMENT
Topic
Name
1+3 =