I'm new to Javascript and Node and I'm having MongoDB trouble. So I have managed to successfully connect to my database using the connection string (using mongoose). This is where I'm stuck, what I want to do is retrieve the first record of the database. Seems like a simple task, but maybe I'm not just understanding some syntax or something.
I think the solution involves something to do with Schemas? I haven't been able to get them to work though.
Here is my JavaScript file. Note the username and password have been omitted.
const mongoose = require('mongoose');
//url to connect to database
const url = 'mongodb://<username>:<password>@ds157614.mlab.com:57614/flight_data';
//connect to mongodb
mongoose.connect(url, { useNewUrlParser: true });
mongoose.connection.once('open', function()
{
console.log('Connected successfully');
}).on('error', function(error)
{
console.log('Connection Error: ', error);
});
Here is an example of a record in the database:
{
"_id": {
"$oid": "5c3538d8ae9fb3103c5b2691"
},
"data": {
"plane_id": 2202,
"temperature": 100
},
"airport": "KBUF"
}
I've messed around with mongoose.connection and got some info to display, but not what I wanted. I want the output to just be the first record in the database.