顾名思义,我正在尝试获取MongoDB Atlas内部已经存在的现有数据。数据来自mongoDB Atlas示例数据,而我正在使用sample_mflix数据库。我进行了一些研究,发现必须首先复制模型并命名集合名称。

对于以下内容,我尝试获取电影收藏数据:

//Movie model
const mongoose = require('../db/mongoose')
//const mongoose = require('mongoose)

const moveiesSchema = new mongoose.Schema({
    plot:{
        type:String
    } ,
    genres:[{
        type:String
    }],
    runtime:{
        type:Number
    },
    cast:[{
        type: String
    }],
    num_mflix_comments:{
        type: Number
    },
    title:{
        type: String
    },
    countries:[{
        type: String
    }],
    released:{
        type: Date
    },
    directors:[{
        type: String
    }],
    rated:{
        type: String
    },
    awards:{
        wins:{
            type: Number
        },
        nominations: {
            type: Number
        },
        text: {
            type: String
        }
    },
    lastupdated:{
        type: String
    },
    year: {
        type: Number
    },
    imdb:{
        rating: {
            type: Number
        },
        votes:{
            type: Number
        },
        id:{
            type: Number
        }
    },
    type: {
        type: String
    },
    tomatoes:{
        viewer:{
            rating: {
                type: Number
            },
            numReviews:{
                type: Number
            },
            meter:{
                type: Number
            }
        },
        lastupdated: {
            type:Date
        }
    }


}, {collection: 'movies'});

const movies = mongoose.model('movies', moveiesSchema)

module.exports = movies


这是我的路由器:

//sample_mflix-router.js
const express = require('express')
const router = new express.Router()

const movies = require('../models/movies_model')



//Currently not working
router.get('/questions', (req, res) => {
    const data = movies.findById('573a1390f29313caabcd4135')
    console.log(data)

})


module.exports = router


当我跟随这个家伙时,就知道问题出在哪里:
Getting NULL data from collection from MongoDB Atlas

这个家伙:
How to get data from existing MongoDB database?

我在想,也许我做错了模型,或者我的mongo URL也错了。
这是我的网址连接代码

MONGODB_URI=mongodb+srv://<user>:<password>@cluster0-rebv7.mongodb.net/sample_mflix?retryWrites=true&w=majority


我用sample_mflix换出了test,因为我在另一篇文章中读到,我应该使用数据库名称而不是给定的默认名称“ test”。

当我尝试发出get请求时,我得到的都是null。知道问题是什么,所以我希望你们中的一个能提供帮助。

编辑:
这是我的连接文件:

mongoose.connect(process.env.MONGODB_URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useFindAndModify: false,
    useCreateIndex: true
})

module.exports = mongoose


我很确定这可以连接到MongoDB Atlas,就像它在集群页面上所说的那样。

最佳答案

尝试这个

router.get('/questions', async (req, res) => {
    const data = await movies.findById('573a1390f29313caabcd4135')
    console.log(data)

})


要么

router.get('/questions', (req, res) => {
    movies.findById('573a1390f29313caabcd4135')
    .then(data => {
         console.log(data)
     });
})

关于node.js - 使用mongoose从MongoDB Atlas获取样本数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59851246/

10-11 14:40
查看更多