MongoDB容器连接

9wbgstp7  于 2023-05-28  发布在  Go
关注(0)|答案(1)|浏览(142)

这是我第一次尝试MongoDB。我只是想简单地连接到它成功。我在这里使用W3教程:https://www.w3schools.com/nodejs/nodejs_mongodb.asp
docker-compose.yml

version: '3.1'
services:
  mongo:
    image: mongo
    restart: unless-stopped
    ports:
      - 27017:27017
    volumes:
      - /home/username/mongodb/data:/data/db
  mongo-express:
    image: mongo-express
    restart: unless-stopped
    ports:
      - 8089:8081

有了这个,mongo-express的UI似乎工作得很好。我可以创建一个数据库等等。
但是,这并不起作用:
create_db_test.js

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mytestdb";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  console.log("Database created!");
  db.close();
});

........

node create_db_test.js

这个就挂着命令行上只有一个空行,它什么也不做。未创建数据库。容器的日志输出如下所示:

mongodb-mongo-1          | {"t":{"$date":"2023-05-27T16:04:20.449+00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.18.0.1:39946","uuid":"abbbc8ad-fceb-4a6b-b6e4-12d123a837b7","connectionId":2,"connectionCount":2}}
mongodb-mongo-1          | {"t":{"$date":"2023-05-27T16:04:20.469+00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn2","msg":"client metadata","attr":{"remote":"172.18.0.1:39946","client":"conn2","doc":{"driver":{"name":"nodejs","version":"5.5.0"},"platform":"Node.js v18.12.1, LE","os":{"name":"linux","architecture":"arm64","version":"6.1.25-37.47.amzn2023.aarch64","type":"Linux"}}}}
mongodb-mongo-1          | {"t":{"$date":"2023-05-27T16:04:30.983+00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.18.0.1:60246","uuid":"47895fe4-78f3-4401-a886-4d5ef832c66b","connectionId":3,"connectionCount":3}}
mongodb-mongo-1          | {"t":{"$date":"2023-05-27T16:04:30.985+00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn3","msg":"client metadata","attr":{"remote":"172.18.0.1:60246","client":"conn3","doc":{"driver":{"name":"nodejs","version":"5.5.0"},"platform":"Node.js v18.12.1, LE","os":{"name":"linux","architecture":"arm64","version":"6.1.25-37.47.amzn2023.aarch64","type":"Linux"}}}}

当我使用Ctrl-C中断create_db_test.js时,容器日志表明连接一直处于打开状态,现在已经结束:

mongodb-mongo-1          | {"t":{"$date":"2023-05-27T16:05:10.053+00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn3","msg":"Connection ended","attr":{"remote":"172.18.0.1:60246","uuid":"47895fe4-78f3-4401-a886-4d5ef832c66b","connectionId":3,"connectionCount":2}}
mongodb-mongo-1          | {"t":{"$date":"2023-05-27T16:05:10.053+00:00"},"s":"I",  "c":"-",        "id":20883,   "ctx":"conn2","msg":"Interrupted operation as its client disconnected","attr":{"opId":620}}
mongodb-mongo-1          | {"t":{"$date":"2023-05-27T16:05:10.054+00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn2","msg":"Connection ended","attr":{"remote":"172.18.0.1:39946","uuid":"abbbc8ad-fceb-4a6b-b6e4-12d123a837b7","connectionId":2,"connectionCount":1}}

我也尝试过这个方法(其中“mydb”已经通过mongo-express创建),结果相同:

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://127.0.0.1:27017/";

MongoClient.connect(url, function(err, db) {
    if (err) throw err;
    var dbo = db.db("mydb");
    var myobj = { name: "Company Inc", address: "Highway 37" };
    dbo.collection("customers").insertOne(myobj, function(err, res) {
      if (err) throw err;
      console.log("1 document inserted");
      db.close();
    });
});

我错过了什么?

vyswwuz2

vyswwuz21#

根据@rickhg12hs的建议,对回调的支持已被删除。下面的代码可以工作:

const { MongoClient } = require("mongodb");
const uri = "mongodb://127.0.0.1:27017/";

const client = new MongoClient(uri);

async function run() {
  try {
    console.log("Trying insert....");
    const database = client.db('mydb');
    const customers = database.collection('customers');
    const myobj = { name: "Company Inc", address: "Highway 37" };
    const result = await customers.insertOne(myobj);
    console.log(result);
  } finally {
    // Ensures that the client will close when you finish/error
    await client.close();
    console.log("Done.");
  }
}
run().catch(console.dir);

相关问题