Javascript 显示mongodb节点的结果
我需要在我的数据库mongodb中显示结果 我使用mongodb.cloud atlas我已经创建了数据库现在我需要在我的数据库中显示结果Javascript 显示mongodb节点的结果,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,我需要在我的数据库mongodb中显示结果 我使用mongodb.cloud atlas我已经创建了数据库现在我需要在我的数据库中显示结果 const MongoClient = require('mongodb').MongoClient; const uri = "mongodb+srv://test:test@cluster0.bvhvj.mongodb.net/*****? retryWrites=true&w=majority"; const client
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://test:test@cluster0.bvhvj.mongodb.net/*****?
retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("sample_restaurants");
// perform actions on the collection object
// need show result database in console.log
client.close();
});
在console.log中的示例_restaurants.restaurants中输入预期结果
编辑
我使用你的答案,但我的问题返回错误
MongoError: no primary server available
此代码
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://test:test@cluster0.bvhvj.mongodb.net/****?
retryWrites=true&w=majority";
client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("sample_restaurants").
collection("restaurants").find({}).toArray(function(err,result)
{
if(err) throw err;
console.log(result)
});
});
首先改进代码以修复错误:
let db=await MongoClient.connect(MONGO_URI,{useNewUrlParser:true});
db=等待db.db();
const collection=db.collection(“样本餐厅”)代码>在数据库初始化后编写查询,collection.collection(“餐厅”).find({})
,为什么不先用谷歌搜索一下,然后再问这个问题呢。有关此错误,请参阅我的控制台中的或您的回答超时:MongoError:没有可用的主服务器。请登录MongoDB atlas,将您的IP地址列入白名单。选择“允许从任何地方访问”,然后单击确认。然后再次尝试运行代码。请参阅此视频以供参考()@LamriDjamalIm尝试您的解决方案,但我的问题是类型错误:collection.find不是function@LamriDjamal我更新答案。按照步骤操作。MONGO_URI是您连接的URL。
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://test:test@cluster0.bvhvj.mongodb.net/*****?
retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("sample_restaurants").
collection("restaurants").find({}).toArray(function(err,result)
{
if(err) throw err;
console.log(result)
});
});