ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 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(); }) }) ~~~