从 pymongo 存储的 mongodb 检索数据
我已通过 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码的问题是您没有将
linux_trace
定义为 JavaScript 中的变量。要访问已拥有集合的 mongo 数据库中的模型,您可以运行类似的命令,
我从这个答案中得到了这个: 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
I got this from this answer: https://stackoverflow.com/a/6721306/3173748