启动数据mongodb 创建数据 use mongodbtest => db.user.insert({"name":"eason","age":23,"sex":true})
=>可以查看下 有没有数据成功插入 db.user.find().pretty()
nodejs 新建一个nodejstest.js
var mongodb = require(‘mongodb‘);
var server = new mongodb.Server("127.0.0.1",27001,{});//本地27001端口
var dbName=new mongodb.Db(‘mongotest‘,server,{});
dbName.open(function(error,client){//数据库:mongotest
if(error) throw error;
var collection = dbName.collection(‘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:eason age:23
时间: 2024-10-13 16:18:37