从 pymongo 存储的 mongodb 检索数据

发布于 2025-01-13 15:06:13 字数 648 浏览 2 评论 0原文

我已通过 pymongo 将数据上传到 MongoDB,我想在我的 nodeJs 中检索它。我正在像这样编写 function 但它不起作用。我的集合名称是linux_trace,我的数据库名称是Linux_Trace_db

错误是linux_trace未定义

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丶视觉 2025-01-20 15:06:13

您的代码的问题是您没有将 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);
   });

我从这个答案中得到了这个: https ://stackoverflow.com/a/6721306/3173748

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文