This is my data which get after run query on mongo terminal db.contacts.find();
[ { _id: 59097d937386cc3a4e7b766b,
name: 'Oreo',
email: 'oreo@gmail.com',
education: 'B.tech',
address: 'West Bengal',
mobile_no: '9120507612',
__v: 0 },
{ _id: 5909814236b6663d8575fc1f,
name: 'John',
email: 'john@gmail.com',
education: 'sports',
address: 'New Delhi',
mobile_no: '9234567788',
__v: 0 },
{ _id: 590982281eb9ab3dd5cf54b1,
name: 'Vicky',
email: 'vicky@gmail.com',
education: 'B.Tech',
address: 'Burgeon',
mobile_no: '123456789',
__v: 0 },
Kindly suggest
This can be done by using mongoose. Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment. See: https://www.npmjs.com/package/mongoose for more information and examples.
Steps:
Include mongoose:
var mongoose = require('mongoose');
Connect to database:
mongoose.connect('mongodb://mongo:27017/yourdatabasename');
Define schema:
var contactSchema = mongoose.Schema({
_id: String, //or _id: mongoose.Schema.Types.ObjectId
name: String,
email: String,
education: String,
adress: String,
mobile_no: Number
});
Define model:
var ContactModel = mongoose.model('yourcollectionname', contactSchema);
Extract data and return to webpage on call:
app.get('/getusers', function(req, res) {
ConcactMOdel.find({}}, function(err, foundData) { //empty query for all data
if(err) {
console.log(err);
return res.status(500).send();
} else {
return res.status(200).send(foundData);
}
});
});
On your webpage you can use a AJAX get request to fetch your data, then manipulate it.
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
{
var dataSet = JSON.parse(this.response);
//manipulate/visualise the data as you wish
}
};
xhttp.open("GET", "/getusers", true);
xhttp.send();
Hope this helps