【发布时间】:2018-09-21 21:53:46
【问题描述】:
我正在尝试使用Nodejs 在MongoDB 集合中简单地插入一个条目。这是我的代码:
代码:
const MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/TodoApp', (err, db) => {
if (err) {
return console.log('Unable to connect to MongoDB server');
}
console.log('Connected to MongoDB server');
db.collection('Todos').insertOne({
text: 'Something to do',
completed: false
}, (err, result) => {
if (err) {
return console.log('Unable to insert todo', err);
}
console.log(JSON.stringify(result.ops, undefined, 2));
});
db.close();
});
当我运行代码时,它显示以下错误:
错误:
TypeError: db.collection is not a function
【问题讨论】:
-
您使用的是哪个版本的 Mongo?