How to get Data from MongoDb using mongoose?

how to retrieve data from mongodb using node js
display mongodb data in html table
express & mongodb
get data from two collections in mongoose
mongoose programming
mongoose orm
install mongoose
mongoose docs

I just started learning MongoDB and mongoose. Currently I have the following structure:

database   -> skeletonDatabase
collection -> adminLogin

When I run db.adminLogin.find() from the command line I get:

{ "_id" : ObjectId("52lhafkjasfadsfea"), "username" : "xxxx", "password" : "xxxx" }

My connection (this works, just adding it FYI)

module.exports = function(mongoose)
{
    mongoose.connect('mongodb://localhost/skeletonDatabase');

    var db = mongoose.connection;
    db.on('error', console.error.bind(console, 'connection error:'));
    db.once('open', function callback () {
        console.log('Conntected To Mongo Database');
    });
}

My -js-

module.exports = function(mongoose)
{
    var Schema = mongoose.Schema;

    // login schema
    var adminLogin = new Schema({
        username: String,
        password: String
    });

    var adminLoginModel = mongoose.model('adminLogin', adminLogin);
    var adminLogin = mongoose.model("adminLogin");

    adminLogin.find({}, function(err, data){
        console.log(">>>> " + data );
    });
}

My console.log() returns as >>>>

So what am I doing wrong here? Why do I not get any data in my console log? Thanks in advance for any help.

mongoose by default takes singular model names and pairs them with a collection named with the plural of that, so mongoose is looking in the db for a collection called "adminLogins" which doesn't exist. You can specify your collection name as the 2nd argument when defining your schema:

var adminLogin = new Schema({
    username: String,
    password: String
}, {collection: 'adminLogin'});

How to get Data from MongoDb using mongoose?, mongoose by default takes singular model names and pairs them with a collection named with the plural of that, so mongoose is looking in the  The GET method has one parameter – a callback function. This callback function, in turn, has two parameters – request and response. This is how a GET request is made in mongoose. But currently, this will do nothing. We have to specify a method to fetch the data from the database. There are several methods.

first compile just one model with the schema as an argument

var adminLogin = mongoose.model('adminLogin', adminLogin);

in your code adminLogin does not exist, adminLoginModel does;

after that ,instead to

adminLogin.find({}, function(err, data){
        console.log(">>>> " + data );
    });

try this

adminLogin.find(function (err, adminLogins) {
  if (err) return console.error(err);
  console.log(adminLogins);

is important the "s" because mongo use the plural of the model to name the collection, sorry for my english...

Express Tutorial Part 3: Using a Database (with Mongoose), Mongoose requires a connection to a MongoDB database. You can require() and connect to a locally hosted database with mongoose. connect() , as shown below. //Import the mongoose module var mongoose = require('mongoose'); //Set up default mongoose connection var mongoDB = 'mongodb://127.0.0.1/my_database'; mongoose. Using Mongoose and MongoDb for the LocalLibrary. For the Local Library example (and the rest of this topic) we're going to use the Mongoose ODM to access our library data. Mongoose acts as a front end to MongoDB, an open source NoSQL database that uses a document-oriented data model.

Had a problem with injecting it within an express route for my api so I changed it thanks to @elkhrz by first defining the schema and then compiling that one model I want to then pull like so:

app.get('/lists/stored-api', (req, res) => {

    Apis.find(function(err, apis) {

        if (err) return console.error(err);

        res.send(apis);

    });

});

I wouldn't send it to the body, I would actually do something else with it especially if you plan on making your API a production based application.

Run through this problem and read up on possible proper ways of rendering your data: How to Pass Data Between Routes in Express

Always a good idea to practice safe procedures when handling data.

Reading data from MongoDB using Mongoose (Mongoose Part 4 , Reading data from MongoDB using Mongoose (Mongoose Part 4) - Node.js Tutorial 13 Duration: 5:11 Posted: Oct 7, 2015 NodeJS/Express – POST/GET data to/from MongoDB using Mongoose – Ajax JQuery + Bootstrap form example In the tutorial, we show you how to POST/GET form data to/from MongoDB in NodeJS/Express application using Mongoose with Ajax JQuery & Bootstrap view.

Use mongoose and Express to GET all Users from the MongoDB , Use mongoose and Express to GET all Users from the MongoDB Applications in Duration: 1:58 Posted: Nov 4, 2016 Use Mongoose to add and retrieve data from MongoDB Atlas database To work with MongoDB, we need to define the shape of our data (the Schema), connect to the database, run our query and then close the connection, so let’s get started. Define a User schema with Mongoose Create a new file called userSchema.js and add the following;

Node.js, Now add Express.js routes that we will use to query the data: app.get('/find/:query'​, cors(), function(req, res) { var query = req.params.query; Model.find({  The insertMany() method returns the documents that were added to the collection. This is how we use the post method to insert data in mongoose. Conclusion. Thank you for joining us on another Object Rocket knowledge-base tutorial. We have covered how to use post in mongoose to insert data into your MongoDB database.

Mongoose Basics: Reading Data With Node.js And MongoDB, A MongoDB CRUD guide explaining how to read data from a MongoDB database in Node.js using the 'find' function in the Mongoose library. Rest Api to get single record from Mongodb Using Node.js and ExpressJS We will create a GET type rest request to access a single employee record from mongodb. We will use find () method of mongoose and passed desired employee id.We will send json data to client as response object.

Comments
  • It's a terrible anti-feature. I think I've seen talk on github of putting a stop to it in some future release. If you are using express, res.send(data) to send it to the browser and you're done.
  • Nice to know, yeah I am using express. But I am a little foggy on that as well. I tried using res.send(data) but couldn't quiet figure out how the whole response thing was suppose to work. I have my app.configure global along with my mongoose connection. So I am using require() but wasn't sure how to pass a request through it.