


How to change MongoDB user password in Mongo shell?
In Mongo shell db.changeUserPassword() shell method is used to change the password of the currently logged in user. Syntax Example

Dynamic key in MongoDB Node.js Native API
Learn how a key can be dynamic in a MongoDB Node.js native API. In the above code snippet, user_tree is queried using userId. But what if userId is dynamic i.e […]

Unescaped at-sign in authority section – MongoDB connection string error. How to fix?
When your mongodb username or password contains ‘@’ you would get the error Unescaped at-sign in authority section. To fix this you can send the encoded value of @ that […]

Match a value with multiple fields using OR in MongoDB
I have a scenario where I need to allow users to log into an application using either username or email address. Hence I need to match username with both username […]

Rename a collection in MongoDB
Renaming a collection in MongoDB is quite simple. When you rename a collection, it obtains a lock on the collection till the rename process is complete. Syntax Example Here the […]

Slice only one element from an array in MongoDB
Let’s say we have two documents in a MongoDB collection called scores and I would like to fetch the latest score of Ron. I need to fetch the latest element […]

How to check if a field exists in a MongoDB document?
In this quick post, learn how to find out if a field has a value, any value in a document in MongoDB. To do this, we can use $exists operator […]

MongoError: the update operation document must contain atomic operators – How to fix?
How to fix this error when you are working with MongoDB documents. – MongoError: the update operation document must contain atomic operators. ? I run into this error often even […]

Get data from MongoDB in Cypress API testing
In this tutorial let’s see how you can get data from MongoDB and use the data when you test APIs with Cypress framework. Getting data from a database is not […]

How to find duplicate documents in a MongoDB collection?
In this post we will show you how to find duplicate documents in your existing database using MongoDB’s aggregation pipeline. Let’s take a sample employees collection for this post. I’ve […]

Convert MongoDB ObjectId to timestamp and vice versa
Learn how to convert MongoIn any database, either Relational or NOSQL db, in a table/collection, there is a primary key for each row/document which is always unique. In MongoDB it’s […]

MongooseError [MongooseServerSelectionError]: getaddrinfo EAI_AGAIN – How to fix?
In this small post, let’s see how to resolve getaddrinfo EAI_AGAIN error when you try to connect to a database in mongo using Mongoose or other mongo libraries. This error […]

How to query an array in MongoDB?
In this post you will learn how to query an array in a MongoDB collection. You will also learn how to filter a pattern in an array in a document […]

Pattern matching in MongoDB with regex
In this post, you will learn how to match mongodb documents with a partial value. This is similar to pattern matching using LIKE predicate in SQL. Finding a document in […]

The option ‘autoReconnect’ is incompatible with the unified topology – Mongo Error – How to fix?
Find out how to fix the MongoDB error – The option autoReconnect is incompatible with the unified topology. This error is very similar to the other errors you might face […]

Update if exists, insert if it doesn’t exist – How to do Upsert in Mongoose?
To find a document and update it using Mongoose, you can use the findOneAndUpdate schema method. As the name implies, findOneAndUpdate() filter the document first that matches a given filter, […]

MongoError: pool is draining, new operations prohibited – How to fix?
MongoError: pool is draining, new operations prohibited – How to fix? You might face this error while doing some operations on a Mongo database. This error basically means that the […]

Unsupported projection option: sort – Mongoose error – How to fix?
UnhandledPromiseRejectionWarning: MongoError: Unsupported projection option: sort is an error that is thrown when you pass sort options into mongoose schema methods. I passed the sort and limit options inside the […]

Enable Authentication to MongoDB Database
Using MongoDB without enabling authentication and access control lets anybody view and alter your data. It’s okay to work with MongoDB without access control when you are a novice developer […]

Install MongoDB in Ubuntu 18.04 and 19.04
The steps given in this post are tested on Ubuntu 19.04, however, they are the same for other Ubuntu versions such as 18.04, 16.04, 15.04, 14.04. If you want to skip […]

Drop a MongoDB database from the command line
You can drop a database in MongoDB from the command line using db.dropDatabase() command or a one line shell script. Let’s see how to do this. db.dropDatabase() command removes the […]

Pagination using Node.js and MongoDB
A huge set of data can be split into a series of related pages by Pagination and only one page can be loaded and viewed at a time. If you […]

MongoDB Import JSON Error – Unmarshaling Bytes
You can import data from en external JSON file using the following command. There are multiple ways to import data into MongoDB. When you are importing data into a MongoDB […]