这是我在mongo终端上运行后查询的数据 的 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 },
请建议
答案 0 :(得分:2)
这可以通过使用猫鼬来完成。 Mongoose是一个MongoDB对象建模工具,旨在在异步环境中工作。有关更多信息和示例,请参阅:https://www.npmjs.com/package/mongoose。
步骤:
包括猫鼬:
var mongoose = require('mongoose');
连接数据库:
mongoose.connect('mongodb://mongo:27017/yourdatabasename');
定义架构:
var contactSchema = mongoose.Schema({
_id: String, //or _id: mongoose.Schema.Types.ObjectId
name: String,
email: String,
education: String,
adress: String,
mobile_no: Number
});
定义模型:
var ContactModel = mongoose.model('yourcollectionname', contactSchema);
提取数据并在电话中返回网页:
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);
}
});
});
在您的网页上,您可以使用AJAX获取请求来获取数据,然后对其进行操作。
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();
希望这有帮助