Articles on Trending Technologies

Technical articles with clear explanations and examples

How to get the last N records in MongoDB?

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

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 More

How do you remove an array element by its index in MongoDB

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

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 More

Best way to store date/time in MongoDB?

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

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 More

How to remove array element in MongoDB?

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

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 More

MongoDB $push in nested array?

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

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 More

Query for documents where array size is greater than 1 in MongoDB?

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

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 More

How to filter array in subdocument with MongoDB?

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

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 More

Get Random record from MongoDB?

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

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

Get names of all keys in the MongoDB collection?

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

Since MongoDB documents can have different structures (no fixed schema), getting all key names requires iterating over documents. There are multiple approaches depending on whether you need keys from one document or all documents. Method 1: Using JavaScript Loop (Single Document) Use findOne() to get a document and loop through its keys ? var doc = db.studentGetKeysDemo.findOne(); for (var key in doc) { print(key); } Sample Data db.studentGetKeysDemo.insertOne({ "StudentId": 1, "StudentName": "Larry", "StudentAge": 23, ...

Read More

How to query MongoDB with LIKE?

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

MongoDB does not have a LIKE operator like SQL. Instead, use regular expressions (regex) or the $regex operator for pattern matching. Syntax // Using regex literal (equivalent to SQL LIKE '%value%') db.collection.find({"field": /.*value.*/}) // Using $regex operator db.collection.find({"field": {$regex: "value", $options: "i"}}) Create Sample Data db.employee.insertMany([ {"EmployeeName": "John", "EmployeeSalary": 450000}, {"EmployeeName": "Carol", "EmployeeSalary": 150000}, {"EmployeeName": "James", "EmployeeSalary": 550000}, {"EmployeeName": "Jace", "EmployeeSalary": 670000}, {"EmployeeName": "Larry", "EmployeeSalary": 1000000} ]); Query with "like" ...

Read More
Showing 23851–23860 of 61,297 articles
Advertisements