本文介绍了从 MongoDB Atlas 的集合中获取 NULL 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已使用
1.连接代码 -const 猫鼬 = 要求('猫鼬')const uri = "mongodb+srv://:@cluster0-3awwl.mongodb.net/";猫鼬.connect(uri,{数据库名称:'bing_bot'}).catch((e) => {console.log('数据库连接错误', e)})2.型号-const 猫鼬 = 要求('猫鼬')const Student = mongoose.model('student', {学生姓名: {类型:字符串},接触: {类型:字符串},电子邮件: {类型:字符串},否:{类型:字符串},年: {类型:字符串}})module.exports = Student`在此处输入代码`3. 检索数据 -Student.findById(_id).then((data) => {console.log('数据:', 数据)})4. 使用 MongoClientconst uri = "mongodb+srv://:@cluster0-3awwl.mongodb.net/bing_bot"MongoClient.connect(uri, function(err, client) {如果(错误){console.log('连接到 MongoDB Atlas 时发生错误...\n', err);}console.log('已连接...');const collection = client.db("bing_bot").collection("student");//对集合对象执行操作集合.findOne({学生姓名":测试"}).then((d) => {console.log('数据:', d)})const 数据 = collection.find()控制台日志('数据:1',数据)客户端关闭();});
解决方案
这是因为 Connectivity Code
和 Model
中的 mongoose
实例是不同的和不相关的.一个是连接的(Connectivity),但另一个(模型)不是.您必须使用相同的实例,因此导出一个 mongoose
并在需要的地方导入.
//连接性Code.jsconst 猫鼬 = 要求('猫鼬')const uri = "mongodb+srv://:@cluster0-3awwl.mongodb.net/";猫鼬.connect(uri,{数据库名称:'bing_bot'}).catch((e)=>{console.log('数据库连接错误',e)})module.exports = 猫鼬;//<-- 导出//模型.jsconst mongoose = require('./connectivityCode.js')//<-- 导入//其余代码
I have connected my code to MongoDB Atlas using Mongoose... even though the collection has a data in it its shows null in response.
I want to know the exact issue and troubleshoot it, because the collection has the data required
Collection details are in this image:
1. Connectivity Code -
const mongoose = require('mongoose')
const uri = "mongodb+srv://<user>:<password>@cluster0-3awwl.mongodb.net/";
mongoose.connect(uri, {
dbName: 'bing_bot'
}).catch((e) => {
console.log('Database connectivity error ', e)
})
2.Model-
const mongoose = require('mongoose')
const Student = mongoose.model('student', {
StudentName: {
type:String
},
Contact: {
type:String
},
Email: {
type:String
},
BNo: {
type:String
},
Year: {
type:String
}
})
module.exports = Student`enter code here`
3. Retrieve data -
Student.findById(_id).then((data) => {
console.log('data: ', data)})
4. Using MongoCLient
const uri = "mongodb+srv://<user>:<password>@cluster0-3awwl.mongodb.net/bing_bot"
MongoClient.connect(uri, function(err, client) {
if(err) {
console.log('Error occurred while connecting to MongoDB Atlas...\n', err);
}
console.log('Connected...');
const collection = client.db("bing_bot").collection("student");
// perform actions on the collection object
collection.findOne({
"StudentName": "Test"
}).then((d) => {
console.log('data: ', d)
})
const data = collection.find()
console.log('data:1 ', data)
client.close();
});
解决方案
It's because the mongoose
instances in your Connectivity Code
and Model
are different and unrelated. One is connected (Connectivity), but the other (Model) is not. You've to use the same instance so export one mongoose
and import that where required.
// connectivityCode.js
const mongoose = require('mongoose')
const uri = "mongodb+srv://<user>:<password>@cluster0-3awwl.mongodb.net/";
mongoose.connect(uri, {
dbName: 'bing_bot'
}).catch((e)=>{
console.log('Database connectivity error ',e)
})
module.exports = mongoose; // <-- exporting
// model.js
const mongoose = require('./connectivityCode.js') // <-- importing
// rest of the code
这篇关于从 MongoDB Atlas 的集合中获取 NULL 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!