This is my first time trying MongoDB. I'm just trying to simply connect to it successfully. I'm following the W3 tutorial here: 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
With that, the mongo-express UI seems to work fine. I can create a database etc...
but, this does not work:
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();
});
and
node create_db_test.js
and this just hangs. There's just a blank line on the command line and it does nothing. The database is not created. The output from the container looks like this:
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"}}}}
When I interrupt create_db_test.js with Ctrl-C, the container log suggests that the connection has been open the whole time and is now ended:
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}}
What am I missing here?
client.connect()
never gets called, and no errors are thrown).