How to count documents in MongoDB
In mongodb, we can count document in multiple ways. Some ways are deprecated, and some are newly introduced. Let see all possible method to count the documents. 1. db.collection.count() db.collection.count(query,…
In mongodb, we can count document in multiple ways. Some ways are deprecated, and some are newly introduced. Let see all possible method to count the documents. 1. db.collection.count() db.collection.count(query,…
Radom data is useful to test many scenarios and check our code stability in many ways. We can get random documents from given collection. Solution 1: Using $sample Randomly selects…
We can use various approaches to get the last n record from database. Let find out which approach suits you. You can use combination of the sort() , limit() ,skip() to get the…
We have various ways to achieve the result. However, we will discuss here two methods. 1. Distinct db.collection.distinct(field, query, options) Finds the distinct values for a specified field across a…
Mongoose schemas support a timestamps option. If you set timestamps: true, Mongoose will add two properties of type Date to your schema: createdAt: a date representing when this document was createdupdatedAt: a date representing when…
The error is because of the new version of the mongoose i.e version 6.0.6. useNewUrlParser, useUnifiedTopology, useFindAndModify, and useCreateIndex are no longer supported options. Mongoose 6 always behaves as if useNewUrlParser, useUnifiedTopology, and useCreateIndex are true, and useFindAndModify is false. Please remove…
While working on any project where we may have a requirement to check the size of an array or to get an element whose size is greater or less than…
Some time while performing any operation on user like updating user, dropping user or granting or revoking access, we face error like user not found for particular db. Few thing…