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 8776799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:08:24+00:00 2026-06-13T19:08:24+00:00

This question is ‘similar’ to this but I’m asking for alternative (if it exists).

  • 0

This question is ‘similar’ to this but I’m asking for alternative (if it exists).

I have create a db Nums with a collection numbers in the mongo shell.
Using mongoose as the ODM I want to access that collection and list the numbers.

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

mongoose.connect('mongodb://localhost/Nums');
mongoose.model('numbers', new Schema({value: Number}));

mongoose.connection.on('open', function(ref) {
  console.log('Connected to mongo server.');
});

mongoose.connection.on('error', function(err) {
  console.log('Could not connect to mongo server!');
  console.log(err);
});

var nums = mongoose.model('numbers');
nums.find({}, function(err, data) {console.log(err, data, data.length);});

In order to access an already created database/collections do I always have to go through a mongoose.model and new Schema calls? Can this step be bypassed?

Even though this step has to be written once, it seems that if I have a very large schema this will be very tedious just to pull out a db/collection from mongo.

Is there a work around for this or this is the only path?

  • 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-06-13T19:08:26+00:00Added an answer on June 13, 2026 at 7:08 pm

    After some experimenting the answer is “you have to specify the schema and model, but it’s not that bad”.

    For instance, I could of done:

    mongoose.connect('mongodb://localhost/Nums');
    mongoose.model('numbers', new Schema());
    

    but as @JohnnyHK mentions you miss out on the field type casting.

    Also, suppose that you have a large schema, you can specify what you want to type-cast:

    mongoose.connect('mongodb://localhost/Nums');
    mongoose.model('numbers', new Schema({username: String, address: String}));
    

    I only type-casted two fields.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is similar to this one but with a difference: I have eight
This question is specific to Erlang, but may have general implications to other IO
This question is somewhat similar to Google Spreadsheet multiple column filter using OR but
This question is similar in concept to this one , except I see I
This question is related to hooking with Mobile Substrate, but as long as you
This question might seem really silly to most of the enlightened folks here. But
This question is not strictly related to Symfony 2, but as I use Symfony
This question may be seen as subjective, but I'd like to ask SO users
This question have been answered. I recommend sumit_programmers solution below. For now, I've removed
This question is bit specific for Joomla. I have a main menu consisting of:

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.