atlas.find返回空数组,而本地mongodb正常运行。

huangapple go评论58阅读模式
英文:

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:

&quot;mongodb+srv://&lt;atlas username&gt;:&lt;password&gt;@cluster0.yhy4bwu.mongodb.net/&lt;database name&gt;?retryWrites=true&amp;w=majority&quot;;

example:

 const uri = &quot;mongodb+srv://&lt;atlas username&gt;:&lt;password&gt;@cluster0.yhy4bwu.mongodb.net/nhl?retryWrites=true&amp;w=majority&quot;;

huangapple
  • 本文由 发表于 2023年7月20日 10:27:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726298.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定