Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7412705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:35:01+00:00 2026-05-29T06:35:01+00:00

I am trying to create a simple MongooseJS example program that gets a list

  • 0

I am trying to create a simple MongooseJS example program that gets a list of items from a collection, and it’s coming back empty every time. Here is the code:

var mongoose = require('mongoose')
  , Schema = mongoose.Schema;

var sampleSchema = new Schema({
    sampleField    : String
});

var db = mongoose.connect('mongodb://localhost:27017/test');

var sampleCollection = mongoose.model('sampleCollection', sampleSchema);

sampleCollection.find({ } , function (err, items) {
    console.log(items); // outputs []
    console.log(err); // outputs null
    items.forEach( function(item) {
        console.log(item); // does not reach this code
    });
});

I have a default instance of MongoDB running, and this is what I’ve entered in the shell:

> use test
> db.sampleCollection.save({sampleField : "Hello"});
> db.sampleCollection.save({sampleField : "Goodbye"});
> db.sampleCollection.find({});
{ "_id" : ObjectId("4f28944b38b59225012109da"), "sampleField" : "Hello" }
{ "_id" : ObjectId("4f28945138b59225012109db"), "sampleField" : "Goodbye" }

Any idea why my code doesn’t return any data?

Thanks for your help,
Dave

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:35:02+00:00Added an answer on May 29, 2026 at 6:35 am

    mongoose will normalize the name of collection to lowercase and pluralzed. Therefore, you should insert into db.samplecollections instead of db.sampleCollection. (Notice the difference of letter c and s here).

    to test it:

    s = new sampleCollection({sampleField: 'hello'}); // creates a new record
    s.save(function(err) { 
      sampleCollection.find({ } , function (err, items) {
          console.log(items); 
          console.log(err); 
          items.forEach( function(item) {
              console.log(item); 
          });
      });
    });
    

    and it properly prints:

    [ { sampleField: 'hello', _id: 4f28ab4cc9e58f710a000001 } ]
    null
    { sampleField: 'hello', _id: 4f28ab4cc9e58f710a000001 }
    

    then in mongo shell:

    > show collections
    samplecollections          //<<<<<<<<<<<<<< It's all lowercase and pluralized
    system.indexes
    
    > db.samplecollections.find()
    { "sampleField" : "hello", "_id" : ObjectId("4f28ab4cc9e58f710a000001") }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to create simple program that would perform some trivial operation with
Trying to create a simple document that gets the parentNode then applies a background
I am trying to create a simple page that enters data in to a
Just trying to create a simple method that counts up 1 on the tally
Im trying to create a simple loop that creates 50 buttons, adds them to
I am trying to create a simple style that highlights the focused UI Element
Trying to create a simple plugin that simply connects to an ftp site, looks
I am trying to create a simple offline application for personal-use that uses a
I'm trying to create a simple Asp.Net page to read Perfmon counters from a
I am trying to create a simple image slideshow that fades in and out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.