英文:
atlas .find returning empty array while local mongodb works fine
问题
没有错误,控制台日志显示 []
。
const mongoose = require('mongoose');
const uri = "mongodb+srv://:@cluster0.yhy4bwu.mongodb.net/?retryWrites=true&w=majority";
const playerSchema = new mongoose.Schema({
fullname: String,
goals: Number,
assists: Number,
gamesPlayed: String,
shots: Number,
positionCode: String,
plusminus: String,
team: String,
timeOnIcePerGame: Number,
espnId: Number
})
mongoose.connect(uri)
.then(async () => {
model = mongoose.model('PlayersV3', playerSchema, 'PlayersV3')
const doc = await model.find({fullname : {$regex : new RegExp('a', "i")}}).limit(15).sort({goals: -1});
console.log(doc)
}).catch((error) => {
console.log(error)
});
const local_uri = "mongodb://127.0.0.1:27017/nhl"
本地主机 URI 正常工作。
我确保我没有拼错我的 PlayersV3 集合。
任何能指引我朝正确方向的信息都会很有帮助。
英文:
no errors console logs []
const mongoose = require('mongoose');
const uri = "mongodb+srv://:@cluster0.yhy4bwu.mongodb.net/?retryWrites=true&w=majority";
const playerSchema = new mongoose.Schema({
fullname: String,
goals: Number,
assists: Number,
gamesPlayed: String,
shots: Number,
positionCode: String,
plusminus: String,
team: String,
timeOnIcePerGame: Number,
espnId: Number
})
mongoose.connect(uri)
.then(async ()=>{
model = mongoose.model('PlayersV3', playerSchema, 'PlayersV3')
const doc = await model.find({fullname : {$regex : new RegExp('a', "i")}}).limit(15).sort({goals: -1});
console.log(doc)
}).catch((error)=>{
console.log(error)
});
const local_uri = "mongodb://127.0.0.1:27017/nhl"
the localhost uri works
I have made sure that I have not misspelled my PlayersV3 collection
anything that would point me in the right direction helps
答案1
得分: 0
"fixed it by adding in the database name as one of the query parameters" 表示:通过将数据库名称添加为查询参数来修复了它。
英文:
const uri = "mongodb+srv://:@cluster0.yhy4bwu.mongodb.net/Databasename?retryWrites=true&w=majority";
fixed it by adding in the database name as one of the query parameters
答案2
得分: 0
尝试使用此URI,假设您的数据库名称为"nhl",并在URI中添加Atlas用户名和密码。
语法:
"mongodb+srv://<atlas用户名>:<密码>@cluster0.yhy4bwu.mongodb.net/<数据库名称>?retryWrites=true&w=majority";
示例:
const uri = "mongodb+srv://<atlas用户名>:<密码>@cluster0.yhy4bwu.mongodb.net/nhl?retryWrites=true&w=majority";
英文:
try using this one uri
assuming that your db name is "nhl" and also add you atlas username and password in uri.
syntax:
"mongodb+srv://<atlas username>:<password>@cluster0.yhy4bwu.mongodb.net/<database name>?retryWrites=true&w=majority";
example:
const uri = "mongodb+srv://<atlas username>:<password>@cluster0.yhy4bwu.mongodb.net/nhl?retryWrites=true&w=majority";
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论