Articles on Trending Technologies

Technical articles with clear explanations and examples

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 615 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 335 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

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 1K+ 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 899 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
Showing 1–10 of 61,284 articles
« Prev 1 2 3 4 5 6129 Next »
Advertisements