1

I have a CosmosDB collection called plotCasts, which has objects that look like this:

{ ... "owner" : "winery", "grower" : "Bill Jones", ... }

I have the following Mongoose schema:

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

const plotCastSchema = new Schema({
    owner: String,
    grower: String,
   ...
});

const ModelClass = mongoose.model('plotCast', plotCastSchema);

module.exports = ModelClass;

However, when I query the database using the query below, I get an empty array for a result. Any idea why?

PlotCast.find({ owner: 'winery' }).lean().exec(function(err, results) {
                if (err) {
                    res.send(err);
                } else if (!results) {
                    res.send(null);
                } else {
                    res.send(results);
                }
            });
2
  • winery contain database not there that's why it comes empty array Commented Sep 26, 2017 at 10:27
  • @RaviTeja I don't understand-what do you mean? There is a plotcast where the key "owner" has the value of "winery." Commented Sep 26, 2017 at 10:28

2 Answers 2

3

Okay, you named your model plotCast but your collection is plotCasts.

You can force your collection name this way:

const plotCastSchema = new Schema({
    owner: String,
    grower: String,
    ...
}, { collection: 'plotCasts' });

Or, simply define your Model in mongoose with the collection name as first argument, this way:

const ModelClass = mongoose.model('plotCasts', plotCastSchema);

Please let me know if that's it :)

Sign up to request clarification or add additional context in comments.

2 Comments

That works! Thank you. So in the future, the file name of the model should be the same as that of the collection? This is strange, because in my user schema, I am using the name 'user', while the collection is named 'users', and it works fine.
It's not about the file name, it's about the first argument you pass to mongoose.model('plotCast', plotCastSchema); here it is plotCast, which is is wrong as the collection is plotCasts, but if you want can set the collection name in the Schema as i showed in the answer.
1

the problem is naming the db always saves schema in plural form so it should be like below

PlotCasts.find({ owner: 'winery' }).lean().exec(function(err, results) {
            if (err) {
                res.send(err);
            } else if (!results) {
                res.send(null);
            } else {
                res.send(results);
            }
        });

1 Comment

That explains it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.