閱讀一個集合
獲取集合’myCollection’中的所有文件並將其列印到控制檯。
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017/test';
MongoClient.connect(url, function (err, db) {
if (err) throw new Error(err);
var cursor = db.collection('myCollection').find(); // Read method 'find'
cursor.each(function (err, doc) {
if (err) throw new Error(err);
if (doc != null) {
console.log(doc); // Print all documents
} else {
db.close(); // Don't forget to close the connection when you are done
}
});
});
收集方法 find()
db.collection( collection )
.find()
爭論 | 型別 | 描述 |
---|---|---|
collection |
字串 | 指定集合的字串 |