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(); }) }) ~~~