node.js วิธีใช้ mongodb find

ตัวอย่างแบบสั้น ไม่ยาวเหยียดเหมือนที่อื่นจนน่าตกใจ

var MongoClient = require('mongodb').MongoClient;
var host = 'localhost';
var port = '27017';
var db = 'dbname';
MongoClient.connect('mongodb://' + host + ':' + port + '/' + db, function (err, db) {
db.collection('contact').find({}, function (err, result) {
      if (err) {
        console.log(err);
       } else if (result.length) {
        console.log(result);
      } else {
        console.log('No document(s) found with defined "find" criteria!');
      }
}

จะแสดงข้อมูลทั้งหมดที่อยู่ใน collection('contact)

ความคิดเห็น