Using the MongoDb and Mongoose for the first time to store the data of my app.js file. When I run the app.js then it throws this error after a while -> MongooseError: Operation peoples.insertOne() buffering timed out after 10000ms.
import mongoose from "mongoose";
mongoose.set("strictQuery", false);
mongoose.connect(
"mongodb://localhost:27017/peopleDB",
{ useNewUrlParser: true },
(err) => {
if (err) console.log(err);
else console.log("MongoDB is connected");
}
);
const peopleSchema = new mongoose.Schema({
name: String,
age: Number,
});
const People = new mongoose.model("People", peopleSchema);
const people = new People({ name: "John", age: 37 });
people.save();
this is the code that I wrote
After a lot of searching for the solution got to know that the connection was not made, if you are using the node the latest node.js version then replace the "localhost" with "127.0.0.1" and then try running the app.js
Create model name as "person", since people is already plural, and it may cause an error.
And make sure you run mongod server in the background using git
Related
This is the code I've been using in Node.js
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/vegesDB', {
useNewUrlParser: true
});
const vegeSchema = new mongoose.Schema({
name: String,
rating: Number,
review: String
});
const Vege = mongoose.model("Vege", vegeSchema);
const vege = new Vege({
name: "Potato",
rating: 9,
review: "Very versatile vegetable"
});
vege.save();
mongoose.connection.close();
And this is the error message I get in the console:
C:\Users\85569\Desktop\Neptune Pluto\FruitsProject\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:153
const err = new MongooseError(message);
^
MongooseError: Operation `veges.insertOne()` buffering timed out after 10000ms
at Timeout.<anonymous> (C:\Users\85569\Desktop\Neptune Pluto\FruitsProject\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:153:23)
at listOnTimeout (node:internal/timers:564:17)
at process.processTimers (node:internal/timers:507:7)
Node.js v18.6.0
For the record, I'm using MongoDB version v5.0.9
I have no other problems with the version of MongoDB I have loaded on my laptop. It's when I try to use Mongoose that everything goes haywire. Any information about the latest super-duper updated way of using Mongoose for this purpose would be greatly appreciated.
I've tried using variations of the above code suggested by other programmers on other sites and, to date, I haven't found one of them that works.
save() is a method on a Mongoose document. The save() method is asynchronous, so it returns a promise that you can await.
When you create an instance of a Mongoose model using new, calling save() makes Mongoose insert a new document.
In your case, you missed the await part so the mongoose failed to save the data.
to solve this issue use await vege.save()
(i.e)
const Vege = mongoose.model("Vege", vegeSchema);
const vege = new Vege({
name: "Potato",
rating: 9,
review: "Very versatile vegetable"
});
await vege.save();
More about save() can be found here
There's nothing wrong is your code. Just add await before vege.save();.
Like this;
await vege.save();
Update:
Ok so I played with your code and found out commenting //mongoose.connection.close(); these lines makes the code work.
I think since nodejs is asynchronous the save() method takes time so before that finishes the mongoose.con.close() hits and close the connection so that you get the error
Update 2:
This is the code I worked;
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/vegesDB', {
useNewUrlParser: true
});
const vegeSchema = new mongoose.Schema({
name: String,
rating: Number,
review: String
});
const Vege = mongoose.model("Vege", vegeSchema);
(async () => {
const vege = new Vege({
name: "Potato",
rating: 9,
review: "Very versatile vegetable"
});
await vege.save();
})()
// mongoose.connection.close();
I have a fairly simple nodejs console app using mongoose, and I am trying to save a document and end the program execution. The problem is that it hangs in a waiting state.
I just found some other questions like this, informing things like trying to close the connection, but I had no luck with the responses. I already tried everything that I found on docs, and answers and nothing worked.
My code is pretty straightforward:
const mongoose = require("mongoose");
let personSchema = new mongoose.Schema(
{
name: {type: String},
age: {type: Number}
},
{ collection: "person" }
);
let personModel = mongoose.model("Person", personSchema);
mongoose.connect("mongodb://localhost:27017/People", {useNewUrlParser: true, useUnifiedTopology: true}).then(
() => {
personModel.create({
name: "Alex",
age: 40
});
},
err => {
console.error("Error", err);
}
);
What I already tried:
Close the connection after create. Results on the error: Topology is closed. Please connect.
Setting keepAlive to false: no effect
Setting bufferCommands to false: no effect
Is there a way to fix this behavior? Any ideas?
I am creating a multi-tenant Saas App. I was advised by many to keep my separate clients on separate databases, for better security and easier management.
How do we connect multiple databases to the Node app?
I know how to make my app run with a single database connection to mongodb, but not sure about multiple connections.
The mongoose docs mentions the following solutions for multiple connections:
export schema pattern (https://mongoosejs.com/docs/connections.html#multiple_connections)
connection pools (which has only up to 5 connections, which may not be ideal as I may have hundreds of clients in the future)
Another way which I tried (and it works!), is connecting to mongodb during a node API call and executing my logic, as shown below. The code below is a test route for registering a user with name and email. dbutils() is a function that I call to connect to mongodb, using mongoose.connect(). I am not sure if this is a good practice to connect during the API call.
router.post('/:db/register', async (req,res, next) => {
const startTime = new Date();
try {
if(!req.body.name) {
throw new Error("Name required");
}
if(!req.body.email) {
throw new Error("Email required");
}
await dbutils(req.params.db);// connect to db
const session = await mongoose.startSession();
session.startTransaction();
const newUser = new User({
name: req.body.name,
email: req.body.email,
})
await newUser.save({session});
await session.commitTransaction();
session.endSession();
const endTime = new Date();
const diff = endTime.getTime() - startTime.getTime();
return res.json({
newUser: {
email: req.body.email,
name: req.body.name
},
db: req.params.db,
timeElapsed: diff,
});
} catch(ex) {
return next(ex);
}
})
My dbutils() code
const mongoose = require('mongoose');
const mongoURI = "mongodb://PC:27017,PC:27018,PC:27019";
module.exports = async function(db) {
try {
await mongoose.connect(
`${mongoURI}/${db}`,
{
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
}
)
} catch(ex) {
throw ex
}
}
I would be very happy for any recommendation or solution to this problem. Thank you very much in advance for your answer.
It is never a good idea to connect to your DB in an API call, you will be wasting a lot of resources, and deplaying the API responses as well.
The best way for you would be connect to multiple databases when Application starts, along with connection pooling configuration.
You can specify which schema belongs to which connection, and maintain separate DB collections.
You can use below code to work with multiple connections, and pooling:
const connection1 = mongoose.createConnection('mongodb://username:password#host1:port1[?options]',{
poolSize: 10
});
const connection2 = mongoose.createConnection('mongodb://username:password#host2:port2[?options]',{
poolSize: 10
});
Models/Schema on connection 1 can be created as below:
//User schema on connection 1
const userSchema = new Schema({ ... });
const UserModel = connection1.model('User', userSchema);
module.exports = UserModel;
Models/Schema on connection 2 can be created as below:
//Product schema on connection 2
const productSchema = new Schema({ ... });
const ProductModel = connection2.model('Product', productSchema);
module.exports = ProductModel;
For better performance, you can also have shared DB clusters for each DB, and use the cluster to connect to your database.
const conn = mongoose.createConnection('mongodb://[username:password#]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]', options);
For detailed information, Please read Mongoose Multiple Connections, and Connection Pooling
My connection code:
const mongoose = require("mongoose");
//ES6 Promise
mongoose.Promise = global.Promise;
//connect to mongoDB
mongoose.connect("mongodb://localhost/smslist", {
useNewUrlParser: true,
useUnifiedTopology: true,
});
mongoose.connection
.once("open", function () {
console.log("Connected to database successfuly");
})
.on("error", function (error) {
console.log(`DB Connection error:`, error);
});
The mongod server terminal shows that the connection is successfull
2020-04-27T15:35:24.500+0300 I NETWORK [listener] connection accepted from 127.0.0.1:61286 #1 (1 connection now open)
2020-04-27T15:35:24.506+0300 I NETWORK [conn1] received client metadata from 127.0.0.1:61286 conn1: { driver: { name: "nodejs|Mongoose", version: "3.5.6" }, os: { type: "Windows_NT", name: "win32", architecture: "x64", version: "10.0.18362" }, platform: "'Node.js v12.16.2, LE (unified)", version: "3.5.6|5.9.10" }
The node terminal also prints
Connected to database successfuly
But when I try to use a mongoDB UI like robomongo/studio 3t/mongodb compass community, I will not see a database named smslist as expected.
I also realized that if I close the db server terminal the node terminal still shows connected to database successfully and not DB Connection error:, error as expected.
any idea?
If you haven't created any Model yet in this "smslist" database then you can not see any thing about this
You need to create some Model for this "smslist" database then you can see this in your db.
I agree it depends on the code that follows the above.
I've been working through a tutorial on this.
Here is the code and my notes on what finally worked.
// from scratch tutorial
// WORKS!!!!
//require mongoose - works
const mongoose = require('mongoose');
//define constant - works
const url = 'mongodb://127.0.0.1:27017/fruit'
// connect to datbase works as evidenced by message
mongoose.connect(url, { useNewUrlParser: true });
const db = mongoose.connection
db.once('open', _ => {
console.log('Database connected:', url)
});
db.on('error', err => {
console.error('connection error:', err)
});
//above works!! but do not see new database in mongodb
//now add schema
const Schema = mongoose.Schema
// define schema
fruitSchema = new Schema({
name: String,
rating: Number,
review: String
});
// create the model - or "collection" use singular - mongoose makes it plural
// inside database
const Fruit = mongoose.model("Fruit", fruitSchema);
//now create the document from the model (note upper and lower case)
const fruit = new Fruit({
name: "Pear",
rating: 10,
review: "good with Brie"
});
fruit.save();
//close the connection works but causes an error when trying to add a document
// mongoose.connection.close();
I cant create my database 'sms-dev' in mongodb after starting the server but in my console it was printing connected to mongo successfully.I kept my db module inside models folder as db.js and export that module in another file(config.js) inside models folder
Here is the code in db.js file
var db = {
// Connects to mongoDB
connect: function(url, options) {
mongoose.connect(url, options);
mongoose.connection.on('open', function(){
console.log("Connected to mongo successfully");
});
mongoose.connection.on('disconnect', function(){
console.log("Mongo disconnected");
});
mongoose.connection.on('error',function (err) {
console.log('Mongoose default connection error: ' + err);
});
process.on('SIGINT', function() {
mongoose.connection.close(function () {
console.log('Mongoose default connection disconnected through app termination');
process.exit(0);
});
});
}
}
module.exports = db;
and my config.js file is
exports.database = {
url: 'mongodb://127.0.0.1:27017/sms-dev',
options: {
db: { native_parser: true,safe:true },
server: { poolSize: 10 }
}
}
I connected this db in server.js as
var dbcon = process.env.MONGOLAB_URI || config.database.url;
db.connect(dbcon, config.database.options);
This line:
As soon as you create a record with that connection
From this answer.
Did it for me. In my case I had to manually:
Create the database
Create a collection
Create a record in the collection
And then everything was good again.
You should keep your db connection very simple as you are using mongoose.
mongoose.connect should only be called once. That will create the default connection pool for your application.
//db.js
// Bring Mongoose into the app
var mongoose = require( 'mongoose' );
// Create the database connection
mongoose.connect('mongodb://127.0.0.1:27017/sms-dev');
// CONNECTION EVENTS
// When successfully connected
mongoose.connection.on('connected', function () {
console.log('Mongoose default connection open to ' + 'mongodb://127.0.0.1:27017/sms-dev');
});
// If the connection throws an error
mongoose.connection.on('error',function (err) {
console.log('Mongoose default connection error: ' + err);
});
// When the connection is disconnected
mongoose.connection.on('disconnected', function () {
console.log('Mongoose default connection disconnected');
});
// If the Node process ends, close the Mongoose connection
process.on('SIGINT', function() {
mongoose.connection.close(function () {
console.log('Mongoose default connection disconnected through app termination');
process.exit(0);
});
});
You can then easily use the db connection by require mongoose in your files.
//users.js
var mongoose = require( 'mongoose' ),
Users = mongoose.model('users');
It seems that my mongodb was not locked properly so i remove the mongodb.lock file and run with -repair option
Once you ll save data in the database
,You can see the Database by running command
show dbs
const mongoose=require("mongoose")
mongoose.connect('mongodb://localhost:27017/username_db');
var db=mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("connected successfully")
});
const Schema=mongoose.Schema;
const myschema=new Schema({
name:String,
},{
timestamps:true
});
var model=mongoose.model('myname',myschema);
var data=new myname({
name: 'xyz',
})
data.save((err)=>{
res.send("Error in saving to database");
})
Once you ll save data in the database
,You can see the Database by running command
show dbs
const mongoose=require("mongoose")
mongoose.connect('mongodb://localhost:27017/username_db');
var db=mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("connected successfully")
});
const Schema=mongoose.Schema;
const myschema=new Schema({
name:String,
},{
timestamps:true
});
var model=mongoose.model('myname',myschema);
var data=new myname({
name: 'xyz',
})
data.save((err)=>{
res.send("Error in saving to database");
})
This is quite old Post, to which I am updating using latest packages - This solution will fix this mongo db connection issue on following version of env.
Node version - 11.5.0 (node -v)
NPM. - 6.4.1(npm -v)
Typescript. - 3.8.3(tsc -v)
Mongodb. - 5.9.15 (package.json)
Following are the steps need to take care in order to fix this -
1. First of all verify all changes in .ts file will be reflecting changes in corresponding .js file. As it was issue with my code it was not being updated.
Run following command and verify .js file
tsc --build tsconfig.json
If js file is not being updated simply delete.js file and run above command. It's pretty simple fix but some time we overlook for it.
Since it's typescript code. So need to copy past below code for verification.
Import * as m from 'mongoose';
export class UserControl {
RegisterUser(){
Const uri = "mongodb://127.0.0.1:27017/User";
m.connect(uri,
{useNewUrlPaerser:true,
useUnifiedTopology:true,
useFindAndModify:true,
useCreateIndex:true });
Let db = m.connection;
Db.once("open",async() =>.
{console.log(connected)});
Db.once("error",async() =>
{console.log(error)});
Const userSchema = new m.schema({
FirstName:string,
Last name:string
});
Const User = m.model('users', userSchema);
Const user = new User({
FName:Andy,
LName:Pat });
Const result = await user.save();
Console.log(result);
}
Run your solution by npm start.
Verify if db collection created?
If not.
First create db with name as "User" in mongodb
Using mongo db compass.
And than try. Still not able to see the collection.
Now need to start two separate console terminal.
go to folder and executive mongo
c:\program file\MongoDb\server\4.2\bin> mongo.exe
on another terminal type mongod ,
it will start your mongo Damon.
Now try. Since this above step will stable connection and show 1 connection active.
Hope this update help.
If you are using Mongoose or MongoClient to connect the mongodb database you will see the database created after you save the first document to the database.
e.g.: the below code will only show connection was successful but it does not create the database 'mydb'
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mydb')
.then(() => console.log('Connected to MongoDB...'))
.catch(err => console.error('Could not connect to MongoDB...', err));
The databse 'mydb' is created only when you save first document record.
const courseSchema = new mongoose.Schema({
name: String,
author: String,
tags: [String],
date: {type: Date, default: Date.now},
isPublished: Boolean
});
const Course = mongoose.model('Course', courseSchema);
async function createCourse(){
const course = new Course({
name: 'Some Course',
author: 'My Name',
tags: ['JavaScript', 'backend'],
isPublished: true
});
const result = await course.save();
console.log(result);
}
createCourse();
This is a duplicate of:
Mongo db that does not exist but shows up in connection
If you insert data your database will be created.