Articles on Trending Technologies

Technical articles with clear explanations and examples

Get a count of total documents with MongoDB while using limit?

Anvi Jain
Anvi Jain
Updated on 14-Mar-2026 818 Views

To get a count of total documents while also limiting the results, use the $facet operator in MongoDB. This allows you to run multiple aggregation pipelines on the same data simultaneously − one for the limited data and another for the total count. Syntax db.collection.aggregate([ { $facet: { data: [{ $limit: number }], totalCount: [{ $count: "total" }] ...

Read More

MongoDB Regex Search on Integer Value?

Krantik Chavan
Krantik Chavan
Updated on 14-Mar-2026 2K+ Views

To perform regex search on integer values in MongoDB, use the $where operator with JavaScript regex testing. This allows pattern matching on numeric fields by converting them to strings during evaluation. Syntax db.collection.find({ $where: "/^yourPattern.*/.test(this.fieldName)" }); Sample Data db.regExpOnIntegerDemo.insertMany([ { "StudentId": 2341234 }, { "StudentId": 123234 }, { "StudentId": 9871234 }, { "StudentId": 2345612 }, { "StudentId": 1239812345 } ]); { ...

Read More

How to sort inner array in MongoDB?

Daniol Thomas
Daniol Thomas
Updated on 14-Mar-2026 3K+ Views

To sort inner arrays in MongoDB, use the aggregation framework with $unwind, $sort, $group, and $project stages. This approach deconstructs the array, sorts the elements, and reconstructs the sorted array. Sample Data db.sortInnerArrayDemo.insertOne({ "EmployeeDetails": { "EmployeeAddress": { "EmployeeCountry": [ { ...

Read More

Querying internal array size in MongoDB?

Krantik Chavan
Krantik Chavan
Updated on 14-Mar-2026 260 Views

To query internal array size in MongoDB, use the $size operator. This operator returns the number of elements in an array field and can be used within aggregation pipelines to count array elements for specific documents. Syntax db.collection.aggregate([ { $group: { _id: yourObjectIdValue, fieldName: { $first: { $size: "$arrayFieldName" } } } ...

Read More

Add new field to every document in a MongoDB collection?

Daniol Thomas
Daniol Thomas
Updated on 14-Mar-2026 2K+ Views

To add new field to every document in a MongoDB collection, you can use $set operator. The syntax is as follows ? Syntax db.yourCollectionName.update({}, { $set: {"yourFieldName": yourValue} }, false, true); Sample Data To understand the above syntax, let us create a collection with some documents. The query to create a collection with documents is as follows ? db.addNewFieldToEveryDocument.insertMany([ {"StudentName": "John", "StudentAddress": "US"}, {"StudentName": "David", "StudentAddress": "UK"}, {"StudentName": "Carol", "StudentAddress": "UK"}, {"StudentName": "Bob", "StudentAddress": "US"} ]); ...

Read More

How to remove a field completely from a MongoDB document?

Daniol Thomas
Daniol Thomas
Updated on 14-Mar-2026 1K+ Views

You can use the $unset operator to remove a field completely from a MongoDB document. The syntax is as follows ? Syntax db.yourCollectionName.update({}, {$unset: {yourFieldName:1}}, false, true); Sample Data To understand the above syntax, let us create a collection with some documents. The query to create a collection with documents is as follows ? db.removeFieldCompletlyDemo.insertOne({"StudentName":"Larry", "StudentFavouriteSubject": ["Java", "C", "C++", "Python"]}); db.removeFieldCompletlyDemo.insertOne({"StudentName":"Mike", "StudentFavouriteSubject": ["Javascript", "HTML5", "CSS3"]}); db.removeFieldCompletlyDemo.insertOne({"StudentName":"Sam", "StudentFavouriteSubject": ["MongoDB", "MySQL", "SQL Server"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ef55a6fd07954a48906a3") } ...

Read More

Converting string to date in MongoDB?

Nancy Den
Nancy Den
Updated on 14-Mar-2026 1K+ Views

To convert the string to date in MongoDB, use the $dateFromString operator within an aggregation pipeline. This operator parses a string and converts it to a proper date object. Syntax db.yourCollectionName.aggregate([ { $project: { anyVariableName: { $dateFromString: { ...

Read More

Group by dates in MongoDB?

Nancy Den
Nancy Den
Updated on 14-Mar-2026 631 Views

You can use the aggregate framework to group by dates in MongoDB. The aggregation pipeline provides powerful date operators to group documents by specific date components like day, month, or year. Sample Data Let us first create a collection with some documents containing date fields ? db.groupByDateDemo.insertMany([ {"UserLoginDateTime": new ISODate()}, {"UserLoginDateTime": new ISODate("2019-01-31T15:20:09.234Z")}, {"UserLoginDateTime": new ISODate("2017-04-21T16:12:13.240Z")}, {"UserLoginDateTime": new ISODate("2016-05-25T19:11:21.130Z")}, {"UserLoginDateTime": new ISODate("2016-05-25T19:11:21.130Z")}, {"UserLoginDateTime": new ISODate()} ]); Display all documents from ...

Read More

How to change the type of a field in MongoDB?

Nancy Den
Nancy Den
Updated on 14-Mar-2026 3K+ Views

MongoDB allows you to change the data type of existing fields using update operations with type conversion functions. Let us convert a string type field to number type using the parseInt() function with an update query. Sample Data First, create a collection with a document containing different data types ? db.changeDataType.insertOne({ "StudentName": "Larry", "StudentAge": 23, "StudentZipCode": "10001", "isProgrammer": false }); { "acknowledged": true, "insertedId": ObjectId("5c6ed4976fd07954a4890694") } Display ...

Read More

Update field in exact element array in MongoDB?

Krantik Chavan
Krantik Chavan
Updated on 14-Mar-2026 349 Views

To update a specific element in a nested array in MongoDB, use the $ positional operator to match the parent array element, then target the nested array element by its index position using dot notation. $ Positional Operator + Index Targeting ActorDetails[0] — Not Matched ActorName: "Johnny Depp" MovieList[0]: "The Tourist" MovieList[1]: "Public Enemy" ...

Read More
Showing 23841–23850 of 61,297 articles
Advertisements