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
How 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 MoreHow to get the last N records in MongoDB?
To get the last N records in MongoDB, you need to use limit() method combined with sorting. The $natural parameter sorts documents in reverse insertion order, and limit() restricts the number of returned records. Syntax db.yourCollectionName.find().sort({$natural:-1}).limit(yourValue); Sample Data To understand the above syntax, let us create a collection with documents. The query to create a collection with documents is as follows ? db.getLastNRecordsDemo.insertMany([ {"EmployeeName": "Maxwell"}, {"EmployeeName": "Carol"}, {"EmployeeName": "Bob"}, {"EmployeeName": "Sam"}, {"EmployeeName": ...
Read MoreHow do you remove an array element by its index in MongoDB
To remove an array element by its index in MongoDB, you can use the $unset and $pull operators in a two-step process. The $unset operator sets the array element at the specified index to null, and the $pull operator removes all null values from the array. Syntax db.yourCollectionName.update({}, {$unset: {"yourArrayListName.yourPosition": yourPositionValue}}); db.yourCollectionName.update({}, {$pull: {"yourArrayListName": null}}); Sample Data Let us create a collection with a document to demonstrate the removal process ? db.removeArrayElements.insertOne({ "StudentName": "Larry", "StudentAge": 23, "TechnicalSubject": ["C", "C++", "Java", ...
Read MoreBest way to store date/time in MongoDB?
There are two different ways by which you can store date/time in MongoDB. In the first approach, you can use Date objects like JavaScript. The Date object is the best way to store date/time in MongoDB. In the second approach, you can use ISODate(). Both methods store dates in MongoDB's native BSON Date type, ensuring optimal performance for date-based operations. Syntax For Date object ? new Date(); For ISODate ? new ISODate(); Method 1: Storing Date/Time Using Date Object To understand the above syntax, let us create a collection ...
Read MoreHow to remove array element in MongoDB?
To remove array elements in MongoDB, you can use the $pull operator along with $in for multiple values or direct value matching. The $pull operator removes all instances of a value or values that match a specified condition from an existing array. Syntax db.yourCollectionName.update({}, {$pull:{yourFirstArrayName:{$in:["yourValue"]}, yourSecondArrayName:"yourValue"}}, {multi:true} ); Sample Data Let us create a collection with a document to understand the concept better ? db.removeArrayElement.insertOne({ "StudentName":"Larry", "StudentCoreSubject":["MongoDB", "MySQL", "SQL Server", "Java"], "StudentFavouriteTeacher":["John", "Marry", "Carol"] }); ...
Read MoreMongoDB $push in nested array?
The $push operator adds elements to an array field. To push into a nested array (an array inside another array), use the $ positional operator to identify the matched parent array element, then target the nested array with dot notation. Sample Data db.nestedArrayDemo.insertOne({ "EmployeeName": "Larry", "EmployeeSalary": 9000, "EmployeeDetails": [{ "EmployeeDOB": new Date("1990-01-21"), "EmployeeDepartment": "ComputerScience", "EmployeeProject": [ ...
Read MoreQuery for documents where array size is greater than 1 in MongoDB?
There are multiple ways to query documents where an array field has more than a certain number of elements in MongoDB. The recommended approaches are $expr with $size, or the dot notation trick with $exists. Sample Data db.students.insertMany([ {"StudentName": "Larry", "Subjects": ["Java", "C", "C++"]}, {"StudentName": "Maxwell", "Subjects": ["MongoDB"]}, {"StudentName": "Carol", "Subjects": ["MySQL", "SQL Server", "PL/SQL"]} ]); Method 1: Using $expr with $size (Recommended) db.students.find({ $expr: { $gt: [{ $size: "$Subjects" }, 1] } }).pretty(); ...
Read MoreHow to filter array in subdocument with MongoDB?
To filter elements within an array subdocument in MongoDB, you can use $unwind + $match in an aggregation pipeline, or the more concise $filter operator (MongoDB 3.2+). Sample Data db.filterArray.insertOne({ "L": [{"N": 1}, {"N": 2}, {"N": 3}, {"N": 4}, {"N": 5}] }); Method 1: Using $unwind + $match + $group Unwind the array, filter matching elements, then regroup ? db.filterArray.aggregate([ { $match: { _id: ObjectId("5c6d63f2734e98fc0a434aeb") } }, { $unwind: "$L" }, { $match: { "L.N": { ...
Read MoreGet Random record from MongoDB?
Use the $sample aggregation stage to get random records from a MongoDB collection. The size parameter specifies how many random documents to return. Syntax db.collectionName.aggregate([{ $sample: { size: N } }]); Create Sample Data db.employeeInformation.insertMany([ {"EmployeeId": 1, "EmployeeName": "Maxwell", "EmployeeAge": 26}, {"EmployeeId": 2, "EmployeeName": "David", "EmployeeAge": 25}, {"EmployeeId": 3, "EmployeeName": "Carol", "EmployeeAge": 24}, {"EmployeeId": 4, "EmployeeName": "Bob", "EmployeeAge": 28}, {"EmployeeId": 5, "EmployeeName": "Sam", "EmployeeAge": 27} ]); Get One Random Record ...
Read More