0

I want to create collection name's "party" and a request getAll data. My model:

const mongoose = require('mongoose');
let PartySchema = new mongoose.Schema(
    {
        partyId: String,
        partyName: String,
    }
);

module.exports = mongoose.model('Party', PartySchema)

And my controller:

const Party = require('./../models/Party')
module.exports = {
    getAll: (req, res, next) => {
        console.log(Party.find());
        Party.find().exec((err, party)=> {
            if (err)
                res.send(err)
            else if (!party)
                res.send(404)
            else
                res.send(party)
            next()            
        })
    },
}

Console.log show me collectionName is "parties" not "party". What's wrong in hear ?

Nguyễn Thắng
  • 319
  • 5
  • 14

2 Answers2

1

mongoose try to infer collection name by making your shema in plural form.

However, you can force your collection name with second parameter:

let PartySchema = new mongoose.Schema(
    {
        partyId: String,
        partyName: String,
    }, 
    { collection: 'your_collection_name' } 
);
Nguyễn Thắng
  • 319
  • 5
  • 14
Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51
1

Mongoose runs through all collection names through a function that pluralises collection names using a fn called toCollectionName. You can see the implementation here: https://github.com/Automattic/mongoose/blob/master/lib/utils.js#L31

Geraint
  • 3,314
  • 3
  • 26
  • 41