建立 Node.js Express 應用程式時,可以使用 mongoose 輕鬆與 MongoDB 建立連線,如果是使用雲端版的 Atlas,選擇 database 的時候,就要設定其 uri。
問題
登入 mongodb atlas 後,要建立連線時,需要複製一個 uri像是:
mongodb+srv://username:<password>@cluster0.xxxxxxx.mongodb.net/?retryWrites=true&w=majority
如果用上面這個連線,同時 mongodb 有許多 databases,你的應用程式可能抓不到你要的資料。
解決
在 .net 的後面加上 /databaseName 即可。
mongodb+srv://username:<password>@cluster0.xxxxxxx.mongodb.net/databaseName?retryWrites=true&w=majority
留言
發佈留言