输入文件:a.html
hello
插入Mongodb:
const fs=require('fs')
const MongoClient=require('mongodb').MongoClient
fs.readFile(
'a.html',
(err,data)=>{
MongoClient.connect('mongodb://localhost:27017/test',(err,db)=>{
collection=db.collection('a')
collection.insert({data:data.toString()},(err,result)=>err?console.log(err):console.log(result))
db.close()
})
}
)
运行结果:
> use test
switched to db test
> db.a.find()
{ "_id" : ObjectId("59cdafc3a160c90af05ca729"), "data" : "\r\n
tml>\r\n\t
\r\n\t\t\r\n\t\r\n\r\n\t
hello
\r\n\r\n