[英]How to connect to Mongo db server remotely through its default ports?
我正在尝试通过默认端口远程连接到 mongo db 服务器以查看 db 内容。 建议我如何远程执行此操作,也可以通过 python 或 node.js。
要远程连接到 mongodb 服务器,您只需要: *. 数据库 URI,其中包括数据库名称和身份验证(如果需要)
连接远程mongodb共享图集集群示例代码如下:
const express = require('express')
const app = express()
var cors = require('cors')
const { MongoClient } = require('mongodb')
const uri = 'mongodb+srv://xxx-sampledb:xxx-mongodb-
sampledb@sandbox.xxxx.mongodb.net/ellasShop?authSource=admin';
//middleware
app.use(express.json())
app.use(cors())
//initialize db connectivity options
const options = {
useUnifiedTopology: true,
useNewUrlParser: true,
}
app.get('/api/products', async (req, res) => {
const client = new MongoClient(uri, options);
try {
await client.connect();
const database = client.db('ellasShop')
const collection = database.collection('productData')
const products = await collection.findOne();
return res.json(products);
} catch (err) {
console.log(err)
} finally{
await client.close()
}
})
app.listen(5000, () => {
console.log('Server is running')
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.