0

I use mongoose and express library to call API to get mongodb data.

This code i use API can get the data as well if i give up to use Schema

const Driver = require('../models/driver');
const mongoose = require('mongoose');
// If i don't use Schema by using connection can get collection data
const connection = mongoose.connection;

module.exports = {
    gretting (req, res, next) {

        const { theater } = req.query;
        connection.db.collection(theater, (err, collection) => {
            collection.find({}).toArray((err, data) => {
                res.send(data);
            });
        });
    }

If i use Schema, this code i try i will get an empty object

module.exports = {
    gretting (req, res, next) {
        // I set the collection name is Tainan
        Driver.find()
            .then(movie => res.send(movie))
            .catch(next);
    }

Here is my Shecma code:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const PointSchema = new Schema({
    type: { type: String, default: 'Point'},
    coordinates: { type: [Number], index: '2dsphere'}
});

const MovieSchema = new Schema({
    theater: String,
    phone: String,
    movie: {
        film: {
            moviePhoto: [],
            movieStills: [],
            movieActorPhoto: [],
            movieActorCn: [],
            movieDate: [],
            versionType: [],
            cnName: [],
            movieTime: [],
            movieContent: [],
            movieStyle: [],
            releasedTime: [],
            enName: []
        }
    },
    theaterPhoto: String,
    address: String,
    theaterCn: String,
    geometry: PointSchema
});

const Driver = mongoose.model('Tainan', MovieSchema);

module.exports = Driver;

enter image description here

This my main file app.js using promise:

const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const routes = require('./routes/routes');
const app = express();

mongoose.Promise = global.Promise;
if (process.env.NODE_ENV !== 'test') {
    mongoose.connect('mongodb://localhost/movies');
}
app.use(bodyParser.json());
routes(app);
app.use((err, req, res, next) => {
    res.status(422).send({ error: err.message });
});

module.exports = app;

Any one knows how to get my collection data in my case ?

Also if i want to get the specified field data what should i do ?

I try this code but its still an empty object

module.exports = {
    gretting (req, res, next) {
        Driver.find({ "theater": "TodayTainan" })
            .then(movie => res.send(movie))
            .catch(next);
    }

Any help would be appreciated. Thanks in advance.

Morton
  • 5,380
  • 18
  • 63
  • 118
  • Possible duplicate of https://stackoverflow.com/questions/5794834/how-to-access-a-preexisting-collection-with-mongoose – chridam Mar 13 '18 at 13:53
  • 2
    Documentation here http://mongoosejs.com/docs/guide.html#collection – chridam Mar 13 '18 at 13:55
  • I have take a reference from this link before, i use query without Schema is from there. But i can't find the way if i use Shema. Is that mean I have to set third argument ? like `const Driver = mongoose.model('Tainan', MovieSchema, 'Tainan');` – Morton Mar 13 '18 at 14:08
  • 3
    God you are right ! The link's answer is that what i need, i don't even think i can set third argument. Thanks for your reply. – Morton Mar 13 '18 at 14:11

0 Answers0