const MongoClient = require('mongodb').MongoClient; const assert = require('assert'); const url = 'mongodb://127.0.0.1:27017'; const dbName = 'jhblog'; MongoClient.connect(url, {useNewUrlParser:true}, function (err, client) { assert.equal(null, err); console.log('Connected successfully to server'); const db = client.db(dbName); db.collection("posts", function (err, collection) { var list = [ {title: "马里奥", tag:"game"}, {title: "node.js", tag:"it"} ]; collection.insert(list, function (err, result) { assert.equal(null, err); client.close(); }); }); });
一、npm install mongodbnode
二、node test.jsmongodb
三、mongo(进入mongo控制台)数据库
四、show dbs(显示数据库列表 )npm
五、use jhblogpost
六、show collections(显示当前数据库中的集合(相似关系数据库中的表))ui
七、db.posts.find()url
八、exit 退出mongo控制台spa