node.js结合mongodb
一、准备工作
1、在mongodb创建将要读取的表
创建数据库mongotest
use mongotest;
向user表中插入数据
db.user.insert({ name:'flyoung', age:'18', sex:true });
2、安装node-mongodb-native
npm install mongodb
二、实例(node.js读取mongodb)
参考node-mongodb-native的文档:https://github.com/mongodb/node-mongodb-native
var mongodb = require('mongodb'); var server = new mongodb.Server("127.0.0.1",27017,{});//本地27017端口 new mongodb.Db('mongotest',server,{}).open(function(error,client){//数据库:mongotest if(error) throw error; var collection = new mongodb.Collection(client,'user');//表:user collection.find(function(error,cursor){ cursor.each(function(error,doc){ if(doc){ console.log("name:"+doc.name+" age:"+doc.age); } }); }); });
运行:
node mongodbTest.js
结果:
name:flyoung age:18
相关推荐
lbyd0 2020-11-17
BigYellow 2020-11-16
sushuanglei 2020-11-12
我心似明月 2020-11-09
zhushenghan 2020-11-09
sunnnyduan 2020-10-16
不要皱眉 2020-10-14
xiaohai 2020-09-29
songxiugongwang 2020-09-22
萌亖 2020-09-17
LuckyLXG 2020-09-08
sdmzhu 2020-09-01
mkhhxxttxs 2020-09-16
xiaohai 2020-09-16
newcome 2020-09-09
jaylong 2020-08-19
大秦铁骑 2020-08-19
thatway 2020-08-19