NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
## findOne ### Example >Find the first document in the customers collection: ~~~ const MongoClient = require('mongodb').MongoClient; var url = 'mongodb://localhost:27017/'; MongoClient.connect(url, { useNewUrlParser: true },(err,client)=>{ if(err) throw err; var test = client.db('test'); test.collection('douban').findOne({},(err,res)=>{ if(err) throw err; console.log(res); client.close() }) }) ~~~ ~~~ //取出集合中的第一条数据 ~~~ ## find-toArray ~~~ const MongoClient = require('mongodb').MongoClient; var url = 'mongodb://localhost:27017/'; MongoClient.connect(url, { useNewUrlParser: true },(err,client)=>{ if(err) throw err; var test = client.db('test'); //获取的是一个promise console.log(test.collection('douban').find({}).toArray()) }) ~~~ ~~~ const MongoClient = require('mongodb').MongoClient; var url = 'mongodb://localhost:27017/'; MongoClient.connect(url, { useNewUrlParser: true }, (err, client) => { if (err) throw err; var test = client.db('test'); test.collection('douban').find({}).toArray((err, result) => { if (err) throw err; console.log(result); client.close(); }) }) ~~~