插入MongoDB文檔:mongo控制檯查看插入到MongoDB文檔中的內容


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

相關文章
相關標籤/搜索