Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
Get a count of total documents with MongoDB while using limit?
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 MoreMongoDB Regex Search on Integer Value?
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 MoreHow to sort inner array in MongoDB?
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 MoreQuerying internal array size in MongoDB?
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 MoreAdd new field to every document in a MongoDB collection?
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 MoreHow to remove a field completely from a MongoDB document?
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 MoreConverting string to date in MongoDB?
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 MoreGroup by dates in MongoDB?
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 MoreHow to change the type of a field in MongoDB?
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 MoreUpdate field in exact element array in MongoDB?
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