从 pymongo 存储的 mongodb 中检索数据

Retrieving data from mongodb stored by pymongo

我已通过 pymongo 将数据上传到 MongoDB,我想在我的 nodeJs 中检索它。我正在这样写 function 但它不起作用。我的 collection 名字是 linux_trace 我的 database 名字是 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)})

您的代码存在的问题是您没有将 linux_trace 定义为 javascript 中的变量。

要访问 mongo 数据库中已有集合的模型,您可以 运行 像这样

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

我从这个答案中得到这个: