mongoose find return null when using a different database

I have two database called users and search. They both have a table same as db name. The contents of dbs are as follows.

Database - users - table users

  • name
  • users

Database - search - table search

  • title
  • keywords
  • desc
  • link

I use mongoose to list all the contents of the users table as follows

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/users');
mongoose.model('users', {name: String});
mongoose.model('users').find({}, function(err, doc){
    if (err) throw err;
    console.log(doc);
});

and this works well and returns all users that are in the database but when I use the following code to list all the records in the search table

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/search');
mongoose.model('search', {title: String});
mongoose.model('search').find({}, function(err, doc){
    if (err) throw err;
    console.log(doc);
});

It returns NULL. what could this be the reason.

Really not sure why you are using different databases here, perhaps you a confusing the concept with "collections" of which you can have "many" collections to a single database namespace. This is usually the best pattern to follow for your application, where at least "most" items are collections within the same database if not all.

But where you do indeed have different databases and you actually have a "search" collection in the "search" database , then you seem to be missing something about mongoose models.

The general concept is to consider a "model" to be a "singular" representation of an object. When tied to a "collection" the implied relation to singular is then "plural". So indeed this naming actually refers to a collection named "users":

var User = mongoose.model("User", { "title": String });

So in tying to a "collection", mongoose "pluralizes" the model name as the actual collection name. The practice here is that "users" contains many objects of "User" which is a natural association.

It just so it that in fact you have called your model "users", so this does not actually pluralize by the default rules and simply uses the already plural form. But this is not the case for "search", where in fact mongoose thinks by default that you actually mean the collection name that is "plural" for many "search" objects, and the natural stemming of this is "searches".

So if you already have created a collection named "search" and have data in it, you need to tell mongoose specifically the name of the collection to use rather than it's default assumption that you mean to use "searches". For this, "model" takes a third argument:

mongoose.model("search", { "title": String }, "search")

This tells it to actually use your existing collection "search" rather than "searches".

So you either do this, or otherwise re-name your collections to match the expected best practice form. And do consider keeping these in a singular database unless you have some significant reason to do otherwise.