Home > Software engineering >  Retrieving data from mongodb stored by pymongo
Retrieving data from mongodb stored by pymongo

Time:03-15

I have uploaded data to MongoDB by pymongo and I want to retrieve it in my nodeJs. I am writing function like this but it is not working. my collection name is linux_trace and my database name is Linux_Trace_db.

The error is linux_trace is not defined

const mongoose = require("mongoose")
require('dotenv').config();
const URI = process.env.MONGO_URL;
mongoose.connect(
  URI,
 
  (err) => {
    if (err) throw err;
    console.log('Connected to mongodb');
  }
);

linux_trace.find(function (err, adminLogins) {
    if (err) return console.error(err);
    console.log(adminLogins)})

CodePudding user response:

The issue with your code is that you didn't define linux_trace as a variable in javascript.

To get access to a model in a mongo database that already has a collection, you can run something like this

const query = function (err, adminLogins) {
  if (err) return console.error(err);
  console.log(adminLogins)};
mongoose.connection.db.collection('linux_trace', function (err, collection) {
       collection.find(query).toArray(cb);
   });

I got this from this answer: https://stackoverflow.com/a/6721306/3173748

  • Related