简体   繁体   中英

Collection.find() only works on certain Collections

I have set up a MongoDB with Express and Mongoose to communicate with my Angular App.

I'm currently experiencing the following problem:

There are 3 different Collections so far. When I try to go for Collection.find({}) on 2 of them it works perfectly fine, but when I try to get the entries of the 3rd one I always just get an empty Array ( "[]" NEVER undefined or something always just empty braces).

When I try to fetch the entries in the MongoDB shell cli it works perfectly fine with the exact same command.

So here is my non-working code (the specification is not the problem ({name: 1, _id: 0}), I also tried in a lot of different ways without it):

app.get('/api/retrieveCompany', async (req, res) => {
    Company.find({},{name: 1, _id: 0}, function(err, companies) {
        if(err) {
            res.status(500).json('Error')
        } else {
            console.log(companies)
            res.status(200).json(companies)
        }
    })
})

Code of the method that is working just fine :

app.get('/api/retrieveContent', async (req, res) => {
    Content.find({}, function(err, contents) {
        if(err) {
            res.status(500).json('Error')
        } else {
            console.log(contents)
            res.status(200).json(contents)
        }
    })
})

Rest of code that is used for that method:

Mongoose Schema (Company):

const mongoose = require('mongoose')

const CompanySchema = new mongoose.Schema({
    name: String,
    description: String,
    boss: String,
})

const Company = mongoose.model('Company', CompanySchema)

module.exports = Company

Initiation:

mongoose.connect('mongodb://localhost:27017/angulardb', { useNewUrlParser: true })
.then((err) => console.log('Mongoose up'))

const User = require('./models/users')
const Content = require('./models/content')
const Company = require('./models/company')

Mongoose Schema (Content) ( only for comparison ):

const mongoose = require('mongoose')

const ContentSchema = new mongoose.Schema({
    subject: String,
    customer: String,
    task: String,
    date: Date,
    inCharge: String,
    comment: [String],
})

const Content = mongoose.model('Content', ContentSchema)

module.exports = Content

Response I get from the MongoDB Shell CLI:

> db.company.find({},{name: 1, _id: 0})
{ "name" : "IT Solutions" }
{ "name" : "Microsoft" }
{ "name" : "Company15" }
{ "name" : "New Company" }

According to the mongoose documentation,

Mongoose by default produces a collection name by passing the model name to the utils.toCollectionName method. This method pluralizes the name. Set this option if you need a different name for your collection.

But I saw your collection name is in singular form. You can rename your collection or use this to use different collection name.

const CompanySchema = new mongoose.Schema({
    name: String,
    description: String,
    boss: String,
}, { collection: 'company' })

Hope this might help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM