Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

234
Views
Express + MongoDB

I try to connect an application to mongoDB with express, for the moment i have no problem with the collection users, it works perfectly but it doesn't work for my second collection, when i try to get all data of my second collection, express return me a empty array.

// Import dependencies
const mongoose = require('mongoose');
const express = require('express');
const router = express.Router();

// MongoDB URL from the docker-compose file
const dbHost = 'mongodb://database/mybase';

// Connect to data
mongoose.connect(dbHost);

// create mongoose schema
const userSchema = new mongoose.Schema({
    mail: String,
    mdp: String,
    firstname: String,
    participants: Number
});

// create mongoose schema
const roomSchema = new mongoose.Schema({
    roomId: String,
});

// create mongoose model
const User = mongoose.model('User', userSchema);
const Room = mongoose.model('Room', roomSchema);

/* GET api listing. */
router.get('/', function(req, res) {
    res.send('api works');
});

/* GET all users. */
router.get('/users', function(req, res) {
    User.find({}, function (err, users) {
        if (err) {
            res.status(500).send(error)
        }
        res.status(200).json(users);
    });
});

/* GET all rooms. */
router.get('/room', function(req, res) {
    Room.find({}, function (err, room) {
        if (err) {
            res.status(500).send(error)
        }
        res.status(200).json(room);
    });
});

module.exports = router;

In my mongo data are stored like this :

> db.users.find()
{ "_id" : ObjectId("5903a7ee0e999a5054cc402b"), "mail" : "mail@google.com", "mdp" : "toto", "firstname" : "firstname", "participants" : 100 }
{ "_id" : ObjectId("5903ae7952163fd952970ed5"), "mail" : "mail2@google.com", "mdp" : "toto", "firstname" : "firstname", "participants" : 50 }

> db.room.find()
{ "_id" : ObjectId("59043f280d062a4959b69315"), "roomId" : "6733454" }

I search for a long time but to be honest, i don't know why it doesn't work, i also try to put users data into room data and to copy/paste user schema into room schema, but it doesn't work also.

If i put the User model object in router.get room, it works, i get users data, so for me it's not a problem of route.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

By default, Mongoose applies a function (utils.toCollectionName()) to the name of a model to determine the collection name that Mongoose will use for that model. For a model called Room, that collection name will be rooms.

However, your collection is called room (singular), so there's a mismatch there.

To fix this, you can explicitly specify which collection should be used using the collection schema option:

const roomSchema = new mongoose.Schema({
  roomId: String,
}, {
  collection : 'room'
});
over 4 years ago · Santiago Trujillo Report

0

The problem is that the name of your collection is room, as in singular. The correct name for the collection is rooms, plural.

Mongoose infers from the model name Room that there should be a collection named rooms in the database. If there isn't, it just fails silently and returns an empty array.

If you simply change the name of your collection in the database from room to rooms, your code will work.

You can do so with the renameCollection command.

over 4 years ago · Santiago Trujillo Report

0

Thanks you guys, it was just that and it was in front of me all this times...

It works perfect with collection rooms and not room

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!