How to initiate mongodb replica set using nodejs - node.js

I'm running the following code for mongodb replica set initialization:
try {
const mongoClient: MongoClient = new MongoClient(process.env.MONGODB_URI || 'mongodb://mongodb:27017', {replicaSet: "rs0"});
const mongoDb: Db = new Db(mongoClient, "admin");
const response = await mongoDb.admin().command({ replSetInitiate: {} }, {})
} catch (error) {
console.error(error)
}
However getting an error:
MongoServerSelectionError: Server selection timed out after 30000 ms
And so for any other command
rs.initiate() from mongosh solves the problem for other commands, but I need a way to initiate replica set during the runtime in nodejs
It turns out some kind of deadlock, replica set initialization requires connection to DB, and connection to DB requires replica set initialization
mongodb version: 4.2,
mongodb npm package version: 4.10.0
UPD: startup logs added
I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
W ASIO [main] No TransportLayer configured during NetworkInterface startup
I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=mongodb
I CONTROL [initandlisten] db version v4.2.22
I CONTROL [initandlisten] git version: eef44cd56b1cc11e5771736fa6cb3077e0228be2
I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.1.1 11 Sep 2018
I CONTROL [initandlisten] allocator: tcmalloc
I CONTROL [initandlisten] modules: none
I CONTROL [initandlisten] build environment:
I CONTROL [initandlisten] distmod: ubuntu1804
I CONTROL [initandlisten] distarch: x86_64
I CONTROL [initandlisten] target_arch: x86_64
I CONTROL [initandlisten] options: { net: { bindIp: "*" }, replication: { replSet: "rs0" } }
I STORAGE [initandlisten]
I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=12243M,cache_overflow=(file_max=0M),session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress],
I STORAGE [initandlisten] WiredTiger message [1665036200:416720][1:0x7f8baf77eb00], txn-recover: Set global recovery timestamp: (0, 0)
I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
I STORAGE [initandlisten] No table logging settings modifications are required for existing WiredTiger tables. Logging enabled? 0
I STORAGE [initandlisten] Timestamp monitor starting
I CONTROL [initandlisten]
I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
I CONTROL [initandlisten]
I CONTROL [initandlisten]
I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
I CONTROL [initandlisten] ** We suggest setting it to 'never'
I CONTROL [initandlisten]
I STORAGE [initandlisten] Flow Control is enabled on this deployment.
I STORAGE [initandlisten] createCollection: local.startup_log with generated UUID: bd81a433-4ec3-48df-b67d-e37c47c24343 and options: { capped: true, size: 10485760 }
I INDEX [initandlisten] index build: done building index _id_ on ns local.startup_log
I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
I STORAGE [initandlisten] createCollection: local.replset.oplogTruncateAfterPoint with generated UUID: 0aa8d2bd-f0c7-4703-8641-a029cde77b48 and options: {}
I INDEX [initandlisten] index build: done building index _id_ on ns local.replset.oplogTruncateAfterPoint
I STORAGE [initandlisten] createCollection: local.replset.minvalid with generated UUID: 370687e5-79d9-4365-b6e0-b05cd4ab31f0 and options: {}
I INDEX [initandlisten] index build: done building index _id_ on ns local.replset.minvalid
I STORAGE [initandlisten] createCollection: local.replset.election with generated UUID: 4f892389-a4c5-4abb-b8e6-d5bdb282dc9f and options: {}
I INDEX [initandlisten] index build: done building index _id_ on ns local.replset.election
I REPL [initandlisten] Did not find local initialized voted for document at startup.
I REPL [initandlisten] Did not find local Rollback ID document at startup. Creating one.
I STORAGE [initandlisten] createCollection: local.system.rollback.id with generated UUID: 1049c565-09b2-4bf9-95c3-ade2162773f5 and options: {}
I INDEX [initandlisten] index build: done building index _id_ on ns local.system.rollback.id
I REPL [initandlisten] Initialized the rollback ID to 1
I REPL [initandlisten] Did not find local replica set configuration document at startup; NoMatchingDocument: Did not find replica set configuration document in local.system.replset
I CONTROL [LogicalSessionCacheRefresh] Sessions collection is not set up; waiting until next sessions refresh interval: Replication has not yet been configured
I NETWORK [listener] Listening on /tmp/mongodb-27017.sock
I NETWORK [listener] Listening on 0.0.0.0
I CONTROL [LogicalSessionCacheReap] Sessions collection is not set up; waiting until next sessions reap interval: config.system.sessions does not exist
I NETWORK [listener] waiting for connections on port 27017
I NETWORK [listener] connection accepted from 172.21.0.7:56492 #1 (1 connection now open)
I NETWORK [conn1] received client metadata from 172.21.0.7:56492 conn1: { driver: { name: "nodejs", version: "3.7.3" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "5.10.16.3-microsoft-standard-WSL2" }, platform: "'Node.js v12.22.7, LE (legacy)" }

directConnection option was missing
try {
const mongoClient: MongoClient = new MongoClient(process.env.MONGODB_RO_URI || 'mongodb://mongodb:27017', {replicaSet: "rs0", directConnection: true});
const mongoDb: Db = new Db(mongoClient, "admin");
const response = await mongoDb.admin().command({ replSetInitiate: {} }, {})
} catch (error) {
console.error(error)
}

You're most likely hitting the timeout shown here:
https://mongodb.github.io/node-mongodb-native/3.6/reference/faq/
const mongoClient: MongoClient = new MongoClient(process.env.MONGODB_URI || 'mongodb://mongodb:27017', {replicaSet: "rs0", connectTimeoutMS: SET_VALUE_HERE});

Related

Docker-compose error react-scripts: not found npm ERR! code ELIFECYCLE

This is the error I got from when I type the command docker-compose up. This is my node.js application and I'm using MongoDB. My goal is to containerize this application and publish on docker hub.
1. Creating mongo ... done
2. Creating app ... done
3. Attaching to mongo, app
4. mongo | 2020-07-13T05:02:33.356+0000 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
5. mongo | 2020-07-13T05:02:33.360+0000 W ASIO [main] No TransportLayer configured during NetworkInterface startup
6. mongo | 2020-07-13T05:02:33.360+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=03ce29ac0ecc
7. mongo | 2020-07-13T05:02:33.360+0000 I CONTROL [initandlisten] db version v4.2.8
8. mongo | 2020-07-13T05:02:33.360+0000 I CONTROL [initandlisten] git version: 43d25964249164d76d5e04dd6cf38f6111e21f5f
9. mongo | 2020-07-13T05:02:33.360+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.1.1 11 Sep 2018
10. mongo | 2020-07-13T05:02:33.360+0000 I CONTROL [initandlisten] allocator: tcmalloc
11. mongo | 2020-07-13T05:02:33.360+0000 I CONTROL [initandlisten] modules: none
12. mongo | 2020-07-13T05:02:33.360+0000 I CONTROL [initandlisten] build environment:
13. mongo | 2020-07-13T05:02:33.360+0000 I CONTROL [initandlisten] distmod: ubuntu1804
14. mongo | 2020-07-13T05:02:33.360+0000 I CONTROL [initandlisten] distarch: x86_64
15. mongo | 2020-07-13T05:02:33.360+0000 I CONTROL [initandlisten] target_arch: x86_64
16. mongo | 2020-07-13T05:02:33.360+0000 I CONTROL [initandlisten] options: { net: { bindIp: "*" } }
17. mongo | 2020-07-13T05:02:33.361+0000 I STORAGE [initandlisten]
18. mongo | 2020-07-13T05:02:33.361+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
19. mongo | 2020-07-13T05:02:33.361+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
20. mongo | 2020-07-13T05:02:33.361+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=471M,cache_overflow=(file_max=0M),session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress],
21. mongo | 2020-07-13T05:02:33.863+0000 I STORAGE [initandlisten] WiredTiger message [1594616553:863974][1:0x7fa8ae84db00], txn-recover: Set global recovery timestamp: (0, 0)
22. mongo | 2020-07-13T05:02:33.885+0000 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
23. mongo | 2020-07-13T05:02:33.902+0000 I STORAGE [initandlisten] Timestamp monitor starting
24. mongo | 2020-07-13T05:02:33.910+0000 I CONTROL [initandlisten]
25. mongo | 2020-07-13T05:02:33.910+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
26. mongo | 2020-07-13T05:02:33.910+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
27. mongo | 2020-07-13T05:02:33.910+0000 I CONTROL [initandlisten]
28. mongo | 2020-07-13T05:02:33.911+0000 I STORAGE [initandlisten] createCollection: admin.system.version with provided UUID: 1c3e3ef6-c303-4517-9613-82f840f58488 and options: { uuid: UUID("1c3e3ef6-c303-4517-9613-82f840f58488") }
29. mongo | 2020-07-13T05:02:33.935+0000 I INDEX [initandlisten] index build: done building index _id_ on ns admin.system.version
30. mongo | 2020-07-13T05:02:33.935+0000 I SHARDING [initandlisten] Marking collection admin.system.version as collection version: <unsharded>
31. mongo | 2020-07-13T05:02:33.935+0000 I COMMAND [initandlisten] setting featureCompatibilityVersion to 4.2
32. mongo | 2020-07-13T05:02:33.935+0000 I SHARDING [initandlisten] Marking collection local.system.replset as collection version: <unsharded>
33. mongo | 2020-07-13T05:02:33.935+0000 I STORAGE [initandlisten] Flow Control is enabled on this deployment.
34. mongo | 2020-07-13T05:02:33.935+0000 I SHARDING [initandlisten] Marking collection admin.system.roles as collection version: <unsharded>
35. mongo | 2020-07-13T05:02:33.935+0000 I STORAGE [initandlisten] createCollection: local.startup_log with generated UUID: 03ec4702-b65a-4f88-8080-09ab0b26a7a4 and options: { capped: true, size: 10485760 }
36. mongo | 2020-07-13T05:02:33.954+0000 I INDEX [initandlisten] index build: done building index _id_ on ns local.startup_log
37. mongo | 2020-07-13T05:02:33.955+0000 I SHARDING [initandlisten] Marking collection local.startup_log as collection version: <unsharded>
38. mongo | 2020-07-13T05:02:33.955+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
39. mongo | 2020-07-13T05:02:33.957+0000 I SHARDING [LogicalSessionCacheReap] Marking collection config.system.sessions as collection version: <unsharded>
40. mongo | 2020-07-13T05:02:33.957+0000 I NETWORK [listener] Listening on /tmp/mongodb-27017.sock
41. mongo | 2020-07-13T05:02:33.957+0000 I NETWORK [listener] Listening on 0.0.0.0
42. mongo | 2020-07-13T05:02:33.957+0000 I NETWORK [listener] waiting for connections on port 27017
43. mongo | 2020-07-13T05:02:33.964+0000 I CONTROL [LogicalSessionCacheReap] Sessions collection is not set up; waiting until next sessions reap interval: config.system.sessions does not exist
44. mongo | 2020-07-13T05:02:33.964+0000 I STORAGE [LogicalSessionCacheRefresh] createCollection: config.system.sessions with provided UUID: 4c715ea5-9f5f-41b3-9101-fd44ce5455a4 and options: { uuid: UUID("4c715ea5-9f5f-41b3-9101-fd44ce5455a4") }
45. mongo | 2020-07-13T05:02:33.980+0000 I INDEX [LogicalSessionCacheRefresh] index build: done building index _id_ on ns config.system.sessions
46. mongo | 2020-07-13T05:02:33.997+0000 I INDEX [LogicalSessionCacheRefresh] index build: starting on config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 } using method: Hybrid
47. mongo | 2020-07-13T05:02:33.997+0000 I INDEX [LogicalSessionCacheRefresh] build may temporarily use up to 200 megabytes of RAM
48. mongo | 2020-07-13T05:02:33.997+0000 I INDEX [LogicalSessionCacheRefresh] index build: collection scan done. scanned 0 total records in 0 seconds
49. mongo | 2020-07-13T05:02:33.997+0000 I INDEX [LogicalSessionCacheRefresh] index build: inserted 0 keys from external sorter into index in 0 seconds
50. mongo | 2020-07-13T05:02:34.000+0000 I SHARDING [ftdc] Marking collection local.oplog.rs as collection version: <unsharded>
51. mongo | 2020-07-13T05:02:34.005+0000 I INDEX [LogicalSessionCacheRefresh] index build: done building index lsidTTLIndex on ns config.system.sessions
52. app |
53. app | > main-application#1.0.0 start /usr/src/app
54. app | > concurrently "npm run server" "npm run client"
55. app |
56. app | [1]
57. app | [1] > main-application#1.0.0 client /usr/src/app
58. app | [1] > npm start --prefix view
59. app | [1]
60. app | [0]
61. app | [0] > main-application#1.0.0 server /usr/src/app
62. app | [0] > nodemon mainserver.js
63. app | [0]
64. app | [0] [nodemon] 2.0.4
65. app | [0] [nodemon] to restart at any time, enter `rs`
66. app | [0] [nodemon] watching path(s): *.*
67. app | [0] [nodemon] watching extensions: js,mjs,json
68. app | [0] [nodemon] starting `node mainserver.js`
69. app | [1]
70. app | [1] > main#0.1.0 start /usr/src/app/view
71. app | [1] > react-scripts start
72. app | [1]
73. app | [1] sh: 1: react-scripts: not found
74. app | [1] npm ERR! code ELIFECYCLE
75. app | [1] npm ERR! syscall spawn
76. app | [1] npm ERR! file sh
77. app | [1] npm ERR! errno ENOENT
78. app | [1] npm ERR! main#0.1.0 start: `react-scripts start`
79. app | [1] npm ERR! spawn ENOENT
80. app | [1] npm ERR!
81. app | [1] npm ERR! Failed at the main#0.1.0 start script.
82. app | [1] npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
83. app | [1] npm WARN Local package.json exists, but node_modules missing, did you mean to install?
84. app | [1]
85. app | [1] npm ERR! A complete log of this run can be found in:
86. app | [1] npm ERR! /root/.npm/_logs/2020-07-13T05_02_35_310Z-debug.log
87. app | [1] npm ERR! code ELIFECYCLE
88. app | [1] npm ERR! errno 1
89. app | [1] npm ERR! main-application#1.0.0 client: `npm start --prefix view`
90. app | [1] npm ERR! Exit status 1
91. app | [1] npm ERR!
92. app | npm ERR! Failed at the main-application#1.0.0 client script.
93. app | [1] npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
94. app | [1]
95. app | [1] npm ERR! A complete log of this run can be found in:
96. app | [1] npm ERR! /root/.npm/_logs/2020-07-13T05_02_35_348Z-debug.log
97. app | [1] npm run client exited with code 1
98. app | [0] (node:98) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
99. app | [0] Server is running on port: 5000
100. mongo | 2020-07-13T05:02:36.010+0000 I NETWORK [listener] connection accepted from 172.19.0.3:45254 #1 (1 connection now open)
101. mongo | 2020-07-13T05:02:36.015+0000 I NETWORK [conn1] received client metadata from 172.19.0.3:45254 conn1: { driver: { name: "nodejs", version: "3.5.9" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.19.76-linuxkit" }, platform: "'Node.js v10.21.0, LE (legacy)" }
102. app | [0] MongoDB Connected
Dockerfile
FROM node:10
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
docker-compose.yml
version: '3'
services:
app:
container_name: app
restart: always
build: .
ports:
- '80:3000'
links:
- mongo
mongo:
container_name: mongo
image: mongo
ports:
- '27017:27017'
.dockerignore
node_modules
npm-debug.log
mainserver.js
var express = require('express')
var cors = require('cors')
var bodyParser = require('body-parser')
var app = express()
const mongoose = require('mongoose')
var port = process.env.PORT || 5000
app.use(bodyParser.json())
app.use(cors())
app.use(
bodyParser.urlencoded({
extended: false
})
)
//copy and paste below into mongodb
const mongoURI = 'mongodb://mongo:27017/MainData'
mongoose
.connect(
mongoURI,
{ useNewUrlParser: true }
)
.then(() => console.log('MongoDB Connected'))
.catch(err => console.log(err))
var Users = require('./controller/Users')
var Users2 = require('./controller/Users2')
app.use('/users', Users)
app.use('/users', Users2)
app.listen(port, function() {
console.log('Server is running on port: ' + port)
})
package.json
{
"name": "main-application",
"version": "1.0.0",
"description": "",
"scripts": {
"server": "nodemon mainserver.js",
"client": "npm start --prefix view",
"start": "concurrently \"npm run server\" \"npm run client\""
},
"keywords": [
"nodejs",
"jwt",
"passport",
"express"
],
"author": "",
"license": "ISC",
"dependencies": {
"alert": "^4.1.1",
"bcrypt-nodejs": "0.0.3",
"bcryptjs": "^2.4.3",
"body-parser": "1.19.0",
"compare": "^2.0.0",
"concurrently": "^5.1.0",
"cors": "^2.8.4",
"dotenv": "^8.2.0",
"express": "^4.16.3",
"express-session": "^1.17.1",
"express-validator": "^6.6.0",
"generate-password": "^1.5.1",
"jsonwebtoken": "^8.5.1",
"latest-version": "^5.1.0",
"mongodb": "^3.1.6",
"mongoose": "^5.2.15",
"nodemailer": "^6.4.8",
"nodemon": "^2.0.3",
"truffle": "^5.1.10"
}
}
You have to add create-react-app globally in docker container, i.e, npm install -g create-react-app
FROM node:10
RUN npm install -g create-react-app
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
I found the solution to this. On the root folder, type this npm i react-scripts.

Can't render documents from Mongo DB (w/ Mongoose, Node and Express)

I have a test-website running on localhost:3000 with a local mongo db as well. It was working fine when all of a sudden I can't seem to get the documents from the db to render on the website any more. There's a bunch of things already working which I'll list first:
I have installed mongo, mongoose and all other required packages both globally and saved in the package.json (perhaps there has been an update to one of the packages and now the local and global version don't match?).
The database exists (on /data) and has documents which I can find when I run 'mongo' in my terminal.
The connection works both in the app.js file (code below) as well as throwing no error when I run 'mongod'.
I even tried it with a brand new db which containted a new collection and for which I made a test page that should render the files. This is the code that I'm showing as it is much simpler than my website with multiple pages (and lots of Node). This test website is also not working.
I'm using EJS to render the javascript on the website but that does not seem to be the problem.
Frustrating the hell out of me!
Now for some code:
TEST APP.JS
const express = require('express');
const app = express();
const mongo = require('mongo');
const mongoose = require('mongoose');
const path = require('path');
const bodyParser = require('body-parser');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
mongoose.connect('mongodb://localhost:27017/ScaryMovieNight', {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
},
function(err) {
if (err) {
console.log(err);
} else {
console.info('connection ok');
}
});
let Schema = mongoose.Schema;
let testSchema = new Schema ({
title: String
});
let Test = mongoose.model('test', testSchema);
app.get('/test', (req, res) => {
Test.find()
.then(picture => {
res.render('test', {movies: picture})
// Also tried res.json(picture) which returns an empty object
})
.catch(err => {
console.log(err)
})
});
app.listen(3000, () => {
console.log('Test is working on port 3000.')
});
TEST PAGE
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
This is test.
<li>
<% movies.forEach(picture => { %>
<ul>Test Title Is : <%= picture.title %></ul>
<% }) %>
</li>
</body>
</html>
MONGO DB IN THE SHELL
> show dbs
ScaryMovieNight 0.000GB
admin 0.000GB
config 0.000GB
local 0.000GB
> use ScaryMovieNight
switched to db ScaryMovieNight
> db.movies.find()
{ "_id" : ObjectId("5ee0958d569eb17e88a8c2aa"), "title" : "Evil Dead 2", "__v" : 0 }
{ "_id" : ObjectId("5ee177496a0f9090f811be71"), "title" : "Green Room", "__v" : 0 }
{ "_id" : ObjectId("5ee26794d27e52a16c3ab910"), "title" : "Alien", "__v" : 0 }
{ "_id" : ObjectId("5ee7fee3f1a65aac3aa87dcb"), "title" : "Aliens" }
MONGOD CONNECTION
Macintosh-14:TestDB dariustimmer$ mongod
2020-06-19T00:30:50.643+0200 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2020-06-19T00:30:50.683+0200 W ASIO [main] No TransportLayer configured during NetworkInterface startup
2020-06-19T00:30:50.688+0200 I CONTROL [initandlisten] MongoDB starting : pid=8121 port=27017 dbpath=/data/db 64-bit host=Macintosh-14.local
2020-06-19T00:30:50.688+0200 I CONTROL [initandlisten] db version v4.2.7
2020-06-19T00:30:50.688+0200 I CONTROL [initandlisten] git version: 51d9fe12b5d19720e72dcd7db0f2f17dd9a19212
2020-06-19T00:30:50.688+0200 I CONTROL [initandlisten] allocator: system
2020-06-19T00:30:50.688+0200 I CONTROL [initandlisten] modules: none
2020-06-19T00:30:50.688+0200 I CONTROL [initandlisten] build environment:
2020-06-19T00:30:50.688+0200 I CONTROL [initandlisten] distarch: x86_64
2020-06-19T00:30:50.688+0200 I CONTROL [initandlisten] target_arch: x86_64
2020-06-19T00:30:50.688+0200 I CONTROL [initandlisten] options: {}
2020-06-19T00:30:50.689+0200 I STORAGE [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2020-06-19T00:30:50.690+0200 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=3584M,cache_overflow=(file_max=0M),session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress],
2020-06-19T00:30:51.986+0200 I STORAGE [initandlisten] WiredTiger message [1592519451:986196][8121:0x7fffa59013c0], txn-recover: Recovering log 11 through 12
2020-06-19T00:30:52.254+0200 I STORAGE [initandlisten] WiredTiger message [1592519452:254712][8121:0x7fffa59013c0], txn-recover: Recovering log 12 through 12
2020-06-19T00:30:52.506+0200 I STORAGE [initandlisten] WiredTiger message [1592519452:506030][8121:0x7fffa59013c0], txn-recover: Main recovery loop: starting at 11/42368 to 12/256
2020-06-19T00:30:52.635+0200 I STORAGE [initandlisten] WiredTiger message [1592519452:635022][8121:0x7fffa59013c0], txn-recover: Recovering log 11 through 12
2020-06-19T00:30:52.797+0200 I STORAGE [initandlisten] WiredTiger message [1592519452:797815][8121:0x7fffa59013c0], txn-recover: Recovering log 12 through 12
2020-06-19T00:30:52.854+0200 I STORAGE [initandlisten] WiredTiger message [1592519452:854550][8121:0x7fffa59013c0], txn-recover: Set global recovery timestamp: (0, 0)
2020-06-19T00:30:53.496+0200 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2020-06-19T00:30:53.506+0200 I STORAGE [initandlisten] Timestamp monitor starting
2020-06-19T00:30:53.509+0200 I CONTROL [initandlisten]
2020-06-19T00:30:53.509+0200 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-06-19T00:30:53.509+0200 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2020-06-19T00:30:53.510+0200 I CONTROL [initandlisten]
2020-06-19T00:30:53.510+0200 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2020-06-19T00:30:53.510+0200 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2020-06-19T00:30:53.510+0200 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
2020-06-19T00:30:53.510+0200 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2020-06-19T00:30:53.510+0200 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2020-06-19T00:30:53.510+0200 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2020-06-19T00:30:53.510+0200 I CONTROL [initandlisten]
2020-06-19T00:30:53.520+0200 I SHARDING [initandlisten] Marking collection local.system.replset as collection version: <unsharded>
2020-06-19T00:30:53.522+0200 I STORAGE [initandlisten] Flow Control is enabled on this deployment.
2020-06-19T00:30:53.522+0200 I SHARDING [initandlisten] Marking collection admin.system.roles as collection version: <unsharded>
2020-06-19T00:30:53.522+0200 I SHARDING [initandlisten] Marking collection admin.system.version as collection version: <unsharded>
2020-06-19T00:30:53.525+0200 I SHARDING [initandlisten] Marking collection local.startup_log as collection version: <unsharded>
2020-06-19T00:30:53.525+0200 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2020-06-19T00:30:53.526+0200 I SHARDING [LogicalSessionCacheRefresh] Marking collection config.system.sessions as collection version: <unsharded>
2020-06-19T00:30:53.527+0200 I NETWORK [listener] Listening on /tmp/mongodb-27017.sock
2020-06-19T00:30:53.527+0200 I NETWORK [listener] Listening on 127.0.0.1
2020-06-19T00:30:53.527+0200 I NETWORK [listener] waiting for connections on port 27017
2020-06-19T00:30:53.527+0200 I SHARDING [LogicalSessionCacheReap] Marking collection config.transactions as collection version: <unsharded>
2020-06-19T00:30:54.003+0200 I SHARDING [ftdc] Marking collection local.oplog.rs as collection version: <unsharded>
2020-06-19T00:30:57.328+0200 I NETWORK [listener] connection accepted from 127.0.0.1:50163 #1 (1 connection now open)
2020-06-19T00:30:57.329+0200 I NETWORK [conn1] received client metadata from 127.0.0.1:50163 conn1: { driver: { name: "nodejs|Mongoose", version: "3.5.9" }, os: { type: "Darwin", name: "darwin", architecture: "x64", version: "16.7.0" }, platform: "'Node.js v12.18.0, LE (unified)", version: "3.5.9|5.9.19" }
I have tried a million things but can't seem to render these movie titles any more. Any help would be greatly appreciated it's been a few very frustrating days..
In mongoose, you have called your model "test", but the collection in MongoDb is called "movies". So your search returns nothing. Try changing your code in your express app:
let Test = mongoose.model('Movie', testSchema);
(This is the minimal change to make it work. If you want to further develop this app, it would be better to call everything 'Movie'.)

Docker container mongodb with Nodejs

I'm trying to dockerize my API, but when I start the application with docker-compose file, the mongodb doesn't starts, and the application cannot connect to the database. By running the application tests using docker, the tests run correctly, but when running npm run production, the mongodb service starts and deactivates.
docker-compose.yml
version: '3'
services:
backend:
build:
context: ../
dockerfile: docker/Dockerfile.production
args:
port: ${PORT}
env_file:
- ../.env
restart: always
ports:
- "${PORT}:${PORT}"
environment:
WAIT_HOSTS: database:27017
database:
image: mongo:latest
env_file:
- ../.env
volumes:
- ".${MONGO_DATA_DIR}:${MONGO_DATA_DIR}"
expose:
- 27017
command: "mongod --smallfiles --logpath=${MONGO_LOG_FILE}"
docker-compose.test.yml
version: '3'
services:
backend-test:
build:
context: ../
dockerfile: docker/Dockerfile.test
args:
port: ${PORT}
env_file:
- ../.env
environment:
WAIT_HOSTS: database-test:27017
database-test:
image: mongo:latest
env_file:
- ../.env
volumes:
- ".${MONGO_TEST_DATA_DIR}:${MONGO_TEST_DATA_DIR}"
expose:
- 27017
command: "mongod --smallfiles --logpath=${MONGO_LOG_FILE}"
Dockerfile.production
FROM node:10.12.0-alpine
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.2.1/wait /wait
RUN chmod +x /wait
WORKDIR /home/nodejs/app
ENV NODE_ENV prod
COPY package*.json ./
RUN npm install --only=production
ARG port=80
EXPOSE $port
COPY . ./
CMD /wait && node index.js
Dockerfile.test
FROM docker_backend
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.2.1/wait /wait
RUN chmod +x /wait
ENV NODE_ENV dev
RUN npm install && npm install -g mocha --timeout 10000
ARG port=80
CMD /wait && mocha tests/unitTests.js --exit
index.js
const express = require('express')
const bodyParser = require('body-parser')
const MongoClient = require('mongodb')
const dbName = process.env.NODE_ENV === 'dev' ? 'database-test' : 'database'
const url = `mongodb://${process.env.MONGO_INITDB_ROOT_USERNAME}:${process.env.MONGO_INITDB_ROOT_PASSWORD}#${dbName}:27017?authMechanism=SCRAM-SHA-1&authSource=admin`
const options = {
useNewUrlParser: true,
reconnectTries: 60,
reconnectInterval: 1000
}
const user = require('./routes/user.js')
const port = process.env.PORT || 80
const app = express()
const http = require('http').Server(app)
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true}))
app.use('/api/v1', user)
app.use((req, res) => {
res.status(404)
})
MongoClient.connect(url, options, (err, database) => {
if(err) {
console.log(`FATAL MONGODB CONNECTION ERROR: $(err):$(err.stack)`)
process.exit(1)
}
app.locals.db = database.db('api')
http.listen(port, () => {
console.log("Listening on port " + port)
app.emit('APP_STARTED')
})
})
module.exports = app
package.json
{
"name": "user-register-backend",
"version": "1.0.0",
"description": "A api to register users backend using cpf and cnpj validation",
"main": "index.js",
"scripts": {
"test": "docker-compose -f docker/docker-compose.test.yml up --build --abort-on-container-exit",
"production": "docker-compose -f docker/docker-compose.yml up",
"build": "docker-compose -f docker/docker-compose.yml build"
},
"keywords": [
"express",
"mongo"
],
"author": "Jefferson Bruchado",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.3",
"express": "^4.16.4",
"mongodb": "^3.1.10"
},
"devDependencies": {
"mocha": "^5.2.0",
"supertest": "^3.3.0",
"tape": "^4.9.1"
}
}
Running npm run test, the application works and performs the unit tests, but when I try to run npm run production, the mongodb service does not start, therefore the application does not work.
Error:
> docker-compose -f docker/docker-compose.yml up
Starting docker_database_1 ... done
Starting docker_backend_1 ... done
Attaching to docker_backend_1, docker_database_1
backend_1 | Checking availability of database:27017
backend_1 | Host database:27017 not yet available
database_1 | about to fork child process, waiting until server is ready for connections.
database_1 | forked process: 24
database_1 | 2018-12-29T17:28:25.914+0000 I CONTROL [main] ***** SERVER RESTARTED *****
database_1 | 2018-12-29T17:28:25.918+0000 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
database_1 | 2018-12-29T17:28:25.922+0000 I CONTROL [initandlisten] MongoDB starting : pid=24 port=27017 dbpath=/data/db 64-bit host=f1619d77c7b7
database_1 | 2018-12-29T17:28:25.922+0000 I CONTROL [initandlisten] db version v4.0.5
database_1 | 2018-12-29T17:28:25.922+0000 I CONTROL [initandlisten] git version: 3739429dd92b92d1b0ab120911a23d50bf03c412
database_1 | 2018-12-29T17:28:25.922+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016
database_1 | 2018-12-29T17:28:25.922+0000 I CONTROL [initandlisten] allocator: tcmalloc
database_1 | 2018-12-29T17:28:25.922+0000 I CONTROL [initandlisten] modules: none
database_1 | 2018-12-29T17:28:25.922+0000 I CONTROL [initandlisten] build environment:
database_1 | 2018-12-29T17:28:25.922+0000 I CONTROL [initandlisten] distmod: ubuntu1604
database_1 | 2018-12-29T17:28:25.922+0000 I CONTROL [initandlisten] distarch: x86_64
database_1 | 2018-12-29T17:28:25.922+0000 I CONTROL [initandlisten] target_arch: x86_64
database_1 | 2018-12-29T17:28:25.922+0000 I CONTROL [initandlisten] options: { net: { bindIp: "127.0.0.1", port: 27017, ssl: { mode: "disabled" } }, processManagement: { fork: true, pidFilePath: "/tmp/docker-entrypoint-temp-mongod.pid" }, storage: { mmapv1: { smallFiles: true } }, systemLog: { destination: "file", logAppend: true, path: "/proc/1/fd/1" } }
database_1 | 2018-12-29T17:28:25.948+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=478M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
backend_1 | Host database:27017 not yet available
database_1 | 2018-12-29T17:28:26.837+0000 E STORAGE [initandlisten] WiredTiger error (1) [1546104506:837236][24:0x7f5c86498a40], connection: __posix_open_file, 715: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted Raw: [1546104506:837236][24:0x7f5c86498a40], connection: __posix_open_file, 715: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted
database_1 | 2018-12-29T17:28:26.892+0000 E STORAGE [initandlisten] WiredTiger error (17) [1546104506:892012][24:0x7f5c86498a40], connection: __posix_open_file, 715: /data/db/WiredTiger.wt: handle-open: open: File exists Raw: [1546104506:892012][24:0x7f5c86498a40], connection: __posix_open_file, 715: /data/db/WiredTiger.wt: handle-open: open: File exists
database_1 | 2018-12-29T17:28:26.895+0000 I STORAGE [initandlisten] WiredTiger message unexpected file WiredTiger.wt found, renamed to WiredTiger.wt.1
database_1 | 2018-12-29T17:28:26.898+0000 E STORAGE [initandlisten] WiredTiger error (1) [1546104506:898402][24:0x7f5c86498a40], connection: __posix_open_file, 715: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted Raw: [1546104506:898402][24:0x7f5c86498a40], connection: __posix_open_file, 715: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted
database_1 | 2018-12-29T17:28:26.967+0000 E STORAGE [initandlisten] WiredTiger error (17) [1546104506:967112][24:0x7f5c86498a40], connection: __posix_open_file, 715: /data/db/WiredTiger.wt: handle-open: open: File exists Raw: [1546104506:967112][24:0x7f5c86498a40], connection: __posix_open_file, 715: /data/db/WiredTiger.wt: handle-open: open: File exists
database_1 | 2018-12-29T17:28:26.970+0000 I STORAGE [initandlisten] WiredTiger message unexpected file WiredTiger.wt found, renamed to WiredTiger.wt.2
database_1 | 2018-12-29T17:28:26.972+0000 E STORAGE [initandlisten] WiredTiger error (1) [1546104506:972043][24:0x7f5c86498a40], connection: __posix_open_file, 715: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted Raw: [1546104506:972043][24:0x7f5c86498a40], connection: __posix_open_file, 715: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted
database_1 | 2018-12-29T17:28:26.974+0000 W STORAGE [initandlisten] Failed to start up WiredTiger under any compatibility version.
database_1 | 2018-12-29T17:28:26.974+0000 F STORAGE [initandlisten] Reason: 1: Operation not permitted
database_1 | 2018-12-29T17:28:26.974+0000 F - [initandlisten] Fatal Assertion 28595 at src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp 638
database_1 | 2018-12-29T17:28:26.974+0000 F - [initandlisten]
database_1 |
database_1 | ***aborting after fassert() failure
database_1 |
database_1 |
database_1 | ERROR: child process failed, exited with error number 14
database_1 | To see additional information in this output, start without the "--fork" option.
docker_database_1 exited with code 14
backend_1 | Host database:27017 not yet available
backend_1 | Host database:27017 not yet available
The problem should be that when the server starts, the db is not created yet. You need a script to ensure the server only starts after the database is created.
To more information you have this link https://docs.docker.com/compose/startup-order/
Or a repo that I created with that problem https://github.com/PedroS11/node-postgres-redis-docker

mongod ERROR: child process failed, exited with error number 14

I got the following error when I tried to restart the db after the server (a linux VM) rebooted without shutting down the db first. I saw someone posted the same error over one and half years ago, but the solution proposed there didn't apply to my situation because it's not a yaml config issue (the db had been running for quite a while). I also included the log at the end. Thanks for any help.
sudo mongod --fork --logpath /nas/is1/bin/mongodb/data/db/mongodb.log --dbpath /nas/is1/bin/mongodb/data/db
about to fork child process, waiting until server is ready for connections.
forked process: 20085
ERROR: child process failed, exited with error number 14
output in the log file.
2017-01-19T15:33:45.286-0500 I CONTROL [initandlisten] MongoDB starting : pid=20085 port=27017 dbpath=/data/mongodb/data/db 64-bit host=raboso
2017-01-19T15:33:45.286-0500 I CONTROL [initandlisten] db version v3.2.1
2017-01-19T15:33:45.286-0500 I CONTROL [initandlisten] git version: a14d55980c2cdc565d4704a7e3ad37e4e535c1b2
2017-01-19T15:33:45.286-0500 I CONTROL [initandlisten] allocator: tcmalloc
2017-01-19T15:33:45.286-0500 I CONTROL [initandlisten] modules: none
2017-01-19T15:33:45.286-0500 I CONTROL [initandlisten] build environment:
2017-01-19T15:33:45.286-0500 I CONTROL [initandlisten] distarch: x86_64
2017-01-19T15:33:45.286-0500 I CONTROL [initandlisten] target_arch: x86_64
2017-01-19T15:33:45.286-0500 I CONTROL [initandlisten] options: { processManagement: { fork: true }, storage: { dbPath: "/data/mongodb/data/db" }, systemLog: { destination: "file", path: "/data/mongodb/data/db/mongodb.log" } }
2017-01-19T15:33:45.329-0500 I - [initandlisten] Detected data files in /data/mongodb/data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2017-01-19T15:33:45.346-0500 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=112G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2017-01-19T15:33:54.009-0500 E STORAGE [initandlisten] WiredTiger (-31802) [1484858034:9041][20085:0x7f0fcf72bcc0], file:sizeStorer.wt, WT_SESSION.open_cursor: sizeStorer.wt read error: failed to read 4096 bytes at offset 49152: WT_ERROR: non-specific WiredTiger error
2017-01-19T15:33:54.011-0500 I - [initandlisten] Invariant failure: ret resulted in status UnknownError -31802: WT_ERROR: non-specific WiredTiger error at src/mongo/db/storage/wiredtiger/wiredtiger_size_storer.cpp 67
2017-01-19T15:33:54.022-0500 I CONTROL [initandlisten]
0x12cf722 0x127ac14 0x1266dad 0x1058db2 0x10425ea 0x103f540 0xf679a8 0x93bc91 0x9403b9 0x7f0fce33bb35 0x939829
----- BEGIN BACKTRACE -----
{"backtrace":[{"b":"400000","o":"ECF722"},{"b":"400000","o":"E7AC14"},{"b":"400000",
"o":"E66DAD"},{"b":"400000","o":"C58DB2"},{"b":"400000","o":"C425EA"},{"b":"400000",
"o":"C3F540"},{"b":"400000","o":"B679A8"},{"b":"400000","o":"53BC91"},{"b":"400000",
"o":"5403B9"},{"b":"7F0FCE31A000","o":"21B35"},{"b":"400000","o":"539829"}],
"processInfo":{ "mongodbVersion" : "3.2.1", "gitVersion" : "a14d55980c2cdc565d4704a7e3ad37e4e535c1b2",
"compiledModules" : [], "uname" : { "sysname" : "Linux", "release" : "3.10.0-514.2.2.el7.x86_64",
"version" : "#1 SMP Wed Nov 16 13:15:13 EST 2016", "machine" : "x86_64" },
"somap" : [ { "elfType" : 2, "b" : "400000" }, { "b" : "7FFEF9CD5000", "elfType" : 3 },
{ "b" : "7F0FCF31B000", "path" : "/lib64/librt.so.1", "elfType" : 3 }, { "b" : "7F0FCF117000",
"path" : "/lib64/libdl.so.2", "elfType" : 3 }, { "b" : "7F0FCEE0F000", "path" : "/lib64/libstdc++.so.6",
"elfType" : 3 }, { "b" : "7F0FCEB0D000", "path" : "/lib64/libm.so.6", "elfType" : 3 },
{ "b" : "7F0FCE8F7000", "path" : "/lib64/libgcc_s.so.1", "elfType" : 3 }, { "b" : "7F0FCE6DB000",
"path" : "/lib64/libpthread.so.0", "elfType" : 3 }, { "b" : "7F0FCE31A000", "path" : "/lib64/libc.so.6",
"elfType" : 3 }, { "b" : "7F0FCF523000", "path" : "/lib64/ld-linux-x86-64.so.2", "elfType" : 3 } ] }}
mongod(_ZN5mongo15printStackTraceERSo+0x32) [0x12cf722]
mongod(_ZN5mongo10logContextEPKc+0x134) [0x127ac14]
mongod(_ZN5mongo17invariantOKFailedEPKcRKNS_6StatusES1_j+0xAD) [0x1266dad]
mongod(_ZN5mongo20WiredTigerSizeStorerC1EP15__wt_connectionRKSs+0x222) [0x1058db2]
mongod(_ZN5mongo18WiredTigerKVEngineC2ERKSsS2_S2_mbbb+0x6DA) [0x10425ea]
mongod(+0xC3F540) [0x103f540]
mongod(_ZN5mongo20ServiceContextMongoD29initializeGlobalStorageEngineEv+0x588) [0xf679a8]
mongod(_ZN5mongo13initAndListenEi+0x321) [0x93bc91]
mongod(main+0x149) [0x9403b9]
libc.so.6(__libc_start_main+0xF5) [0x7f0fce33bb35]
mongod(+0x539829) [0x939829]
----- END BACKTRACE -----
2017-01-19T15:33:54.022-0500 I - [initandlisten]
***aborting after invariant() failure
If a system running MongoDB with the WiredTiger storage engine crashes or experiences an unclean shutdown, MongoDB may not be able to recover data files on restart if the crash/shutdown interrupted a WiredTiger checkpoint.
MongoDB cannot automatically recover data files on restart.
Sadly there is no workaround. Either you can restore data from backups or resync from another replica set member.
WiredTiger (-31802) [1484858034:9041][20085:0x7f0fcf72bcc0], file:sizeStorer.wt, WT_SESSION.open_cursor: sizeStorer.wt read error: failed to read 4096 bytes at offset 49152: WT_ERROR: non-specific WiredTiger error
Above error suggets that your database has been corrupted. Repair it by:
mongod --repair --dbpath /path/to/data/db

MongoDB is not reporting to close connections

I am running through the getting started tutorial to evaluate MongoDB for use in production.
Tailing the server logs it showed the following: in the last 5 lines mongod reports it is closing connections but the count for the number of connections open is not decreasing. Is this expected behaviour?
$ tail -f /usr/local/var/log/mongodb/mongo.log
2015-11-29T15:10:13.425+0000 I JOURNAL [journal writer] Journal writer thread started
2015-11-29T15:10:13.425+0000 I CONTROL [initandlisten] MongoDB starting : pid=86710 port=27017 dbpath=/usr/local/var/mongodb 64-bit host=As-MacBook-Air.local
2015-11-29T15:10:13.425+0000 I CONTROL [initandlisten]
2015-11-29T15:10:13.425+0000 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2015-11-29T15:10:13.425+0000 I CONTROL [initandlisten] db version v3.0.7
2015-11-29T15:10:13.425+0000 I CONTROL [initandlisten] git version: nogitversion
2015-11-29T15:10:13.425+0000 I CONTROL [initandlisten] build info: Darwin As-MacBook-Air.local 13.4.0 Darwin Kernel Version 13.4.0: Wed Mar 18 16:20:14 PDT 2015; root:xnu-2422.115.14~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
2015-11-29T15:10:13.425+0000 I CONTROL [initandlisten] allocator: system
2015-11-29T15:10:13.425+0000 I CONTROL [initandlisten] options: { config: "/usr/local/etc/mongod.conf", net: { bindIp: "127.0.0.1" }, storage: { dbPath: "/usr/local/var/mongodb" }, systemLog: { destination: "file", logAppend: true, path: "/usr/local/var/log/mongodb/mongo.log" } }
2015-11-29T15:10:13.435+0000 I NETWORK [initandlisten] waiting for connections on port 27017
2015-11-29T15:14:21.158+0000 I NETWORK [initandlisten] connection accepted from 127.0.0.1:52667 #1 (1 connection now open)
2015-11-29T15:14:21.169+0000 I NETWORK [conn1] end connection 127.0.0.1:52667 (0 connections now open)
2015-11-29T15:14:21.175+0000 I NETWORK [initandlisten] connection accepted from 127.0.0.1:52668 #2 (1 connection now open)
2015-11-29T15:14:21.176+0000 I NETWORK [initandlisten] connection accepted from 127.0.0.1:52669 #3 (2 connections now open)
2015-11-29T15:14:21.176+0000 I NETWORK [initandlisten] connection accepted from 127.0.0.1:52670 #4 (3 connections now open)
2015-11-29T15:14:21.176+0000 I NETWORK [initandlisten] connection accepted from 127.0.0.1:52671 #5 (4 connections now open)
2015-11-29T15:14:21.179+0000 I NETWORK [initandlisten] connection accepted from 127.0.0.1:52672 #6 (5 connections now open)
2015-11-29T15:14:21.183+0000 I NETWORK [conn2] end connection 127.0.0.1:52668 (4 connections now open)
2015-11-29T15:14:21.184+0000 I NETWORK [conn3] end connection 127.0.0.1:52669 (3 connections now open)
2015-11-29T15:14:21.184+0000 I NETWORK [conn4] end connection 127.0.0.1:52670 (2 connections now open)
2015-11-29T15:14:21.184+0000 I NETWORK [conn5] end connection 127.0.0.1:52671 (1 connection now open)
2015-11-29T15:14:21.184+0000 I NETWORK [conn6] end connection 127.0.0.1:52672 (0 connections now open)
2015-11-29T15:25:39.136+0000 I NETWORK [initandlisten] connection accepted from 127.0.0.1:52799 #7 (1 connection now open)
2015-11-29T15:25:39.137+0000 I NETWORK [conn7] end connection 127.0.0.1:52799 (0 connections now open)
2015-11-29T15:25:39.142+0000 I NETWORK [initandlisten] connection accepted from 127.0.0.1:52800 #8 (1 connection now open)
2015-11-29T15:25:39.142+0000 I NETWORK [initandlisten] connection accepted from 127.0.0.1:52801 #9 (2 connections now open)
2015-11-29T15:25:39.144+0000 I NETWORK [initandlisten] connection accepted from 127.0.0.1:52802 #10 (3 connections now open)
2015-11-29T15:25:39.144+0000 I NETWORK [initandlisten] connection accepted from 127.0.0.1:52803 #11 (4 connections now open)
2015-11-29T15:25:39.145+0000 I NETWORK [initandlisten] connection accepted from 127.0.0.1:52804 #12 (5 connections now open)
2015-11-29T15:25:39.153+0000 I INDEX [conn9] allocating new ns file /usr/local/var/mongodb/test.ns, filling with zeroes...
2015-11-29T15:25:39.209+0000 I STORAGE [FileAllocator] allocating new datafile /usr/local/var/mongodb/test.0, filling with zeroes...
2015-11-29T15:25:39.209+0000 I STORAGE [FileAllocator] creating directory /usr/local/var/mongodb/_tmp
2015-11-29T15:25:39.328+0000 I STORAGE [FileAllocator] done allocating datafile /usr/local/var/mongodb/test.0, size: 64MB, took 0.118 secs
2015-11-29T15:25:39.374+0000 I WRITE [conn9] insert test.restaurants query: { address: { street: "2 Avenue", zipcode: "10075", building: "1480", coord: [ -73.9557413, 40.7720266 ] }, borough: "Manhattan", cuisine: "Italian", grades: [ { date: new Date(1412121600000), grade: "A", score: 11 }, { date: new Date(1389830400000), grade: "B", score: 17 } ], name: "Vella", restaurant_id: "41704620", _id: ObjectId('565b18f333b46874536e90de') } ninserted:1 keyUpdates:0 writeConflicts:0 numYields:0 locks:{ Global: { acquireCount: { r: 2, w: 2 } }, MMAPV1Journal: { acquireCount: { w: 8 }, acquireWaitCount: { w: 1 }, timeAcquiringMicros: { w: 179 } }, Database: { acquireCount: { w: 1, W: 1 } }, Collection: { acquireCount: { W: 1 } }, Metadata: { acquireCount: { W: 4 } } } 221ms
2015-11-29T15:25:39.374+0000 I COMMAND [conn9] command test.$cmd command: insert { insert: "restaurants", documents: [ { address: { street: "2 Avenue", zipcode: "10075", building: "1480", coord: [ -73.9557413, 40.7720266 ] }, borough: "Manhattan", cuisine: "Italian", grades: [ { date: new Date(1412121600000), grade: "A", score: 11 }, { date: new Date(1389830400000), grade: "B", score: 17 } ], name: "Vella", restaurant_id: "41704620", _id: ObjectId('565b18f333b46874536e90de') } ], ordered: true } keyUpdates:0 writeConflicts:0 numYields:0 reslen:40 locks:{ Global: { acquireCount: { r: 2, w: 2 } }, MMAPV1Journal: { acquireCount: { w: 8 }, acquireWaitCount: { w: 1 }, timeAcquiringMicros: { w: 179 } }, Database: { acquireCount: { w: 1, W: 1 } }, Collection: { acquireCount: { W: 1 } }, Metadata: { acquireCount: { W: 4 } } } 221ms
2015-11-29T15:25:39.384+0000 I NETWORK [conn8] end connection 127.0.0.1:52800 (4 connections now open)
2015-11-29T15:25:39.384+0000 I NETWORK [conn9] end connection 127.0.0.1:52801 (4 connections now open)
2015-11-29T15:25:39.384+0000 I NETWORK [conn10] end connection 127.0.0.1:52802 (3 connections now open)
2015-11-29T15:25:39.385+0000 I NETWORK [conn11] end connection 127.0.0.1:52803 (2 connections now open)
2015-11-29T15:25:39.385+0000 I NETWORK [conn12] end connection 127.0.0.1:52804 (2 connections now open)
I started the MongoDB server with:
$ mongod --config /usr/local/etc/mongod.conf
Other details:
$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.9.5
BuildVersion: 13F1096
$ mongod -version
db version v3.0.7
git version: nogitversion
$ cat /usr/local/etc/mongod.conf
systemLog:
destination: file
path: /usr/local/var/log/mongodb/mongo.log
logAppend: true
storage:
dbPath: /usr/local/var/mongodb
net:
bindIp: 127.0.0.1
From my experience, MongoDB is holding a queue of connections that it dispenses on request, not each request translates to opening a connection and closing it when response is delivered.
I've experienced a large number of connections when working from multiple services with the same MongoDB and we had to limit within our app the number of created connections and reuse same connections rather than create a new one for each request.
Hope it helps.

Resources