Articles on Trending Technologies

Technical articles with clear explanations and examples

Tutorix - AI Tutor

What should be used to implement MySQL LIKE statement in MongoDB?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 201 Views

To implement MySQL LIKE statement functionality in MongoDB, use the $regex operator. This provides pattern matching capabilities similar to SQL's LIKE operator for text searches. Syntax db.collection.find({ "fieldName": { $regex: "pattern" } }); // Alternative syntax db.collection.find({ "fieldName": /pattern/ }); Sample Data db.likeInMongoDBDemo.insertMany([ { "Name": "Sam" }, { "Name": "John" }, { "Name": "Scott" }, { "Name": "Sean" }, { "Name": "Samuel" } ]); { ...

Read More

Merge two array fields in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 811 Views

To merge two array fields in MongoDB, use the $setUnion operator in an aggregation pipeline. This operator combines arrays and automatically removes duplicates while preserving unique values from both arrays. Syntax db.collection.aggregate([ { $project: { "MergedFieldName": { $setUnion: ["$arrayField1", "$arrayField2"] } ...

Read More

Find the count of users who logged in between specific dates with MongoDB

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 422 Views

To find the count of users who logged in between specific dates in MongoDB, use the count() method with $gte and $lt operators to define the date range filter. Syntax db.collection.count({ "dateField": { "$gte": new Date("start-date"), "$lt": new Date("end-date") } }); Sample Data db.findDataByDateDemo.insertMany([ { "UserName": "John", "UserLoginDate": new ISODate("2019-01-31") }, { "UserName": "Larry", "UserLoginDate": new ISODate("2019-02-01") }, ...

Read More

Calculate the average value in a MongoDB document grouping by null?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 424 Views

To calculate the average value across all documents in a MongoDB collection, use the $group operator with _id: null to group all documents together, then apply the $avg accumulator operator. Syntax db.collectionName.aggregate([ { $group: { _id: null, "averageFieldName": { $avg: "$fieldName" } } } ]); Sample Data ...

Read More

Implement MongoDB $concatArrays even when some of the values are null?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 412 Views

Use the $ifNull operator with $concatArrays in the aggregation framework to concatenate arrays even when some fields are null or missing. The $ifNull operator replaces null values with an empty array, allowing concatenation to proceed successfully. Syntax db.collection.aggregate([ { $project: { concatenatedField: { $concatArrays: [ { $ifNull: ["$array1", []] }, { $ifNull: ["$array2", []] } ...

Read More

MongoDB query check if value in array property?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 2K+ Views

To check if a value exists in an array property in MongoDB, you can use the $in operator for exact matches. This operator searches for documents where the array field contains any of the specified values. Syntax db.collection.find({ "arrayField": { $in: ["value1", "value2"] } }); Sample Data Let's create a collection with sample documents ? db.valueInArrayDemo.insertMany([ { "UserName": "John", "UserMessage": ["Hi", "Hello", "Bye"] }, ...

Read More

How do I push elements to an existing array in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 508 Views

To push elements to an existing array in MongoDB, use the $addToSet or $push operators with the update() method. The $addToSet adds elements only if they don't already exist, while $push always adds elements regardless of duplicates. Syntax // Using $addToSet (prevents duplicates) db.collection.update( {query}, { $addToSet: { "arrayField": "newElement" } } ); // Using $push (allows duplicates) db.collection.update( {query}, { $push: { "arrayField": "newElement" } } ); Sample Data Let us create a collection with a ...

Read More

How to print document value in MongoDB shell?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 2K+ Views

To print specific document values in MongoDB shell, use the forEach() method combined with the print() function. This allows you to extract and display individual field values from query results. Syntax db.collection.find(query, projection).forEach(function(document) { print(document.fieldName); }); Sample Data db.printDocumentValueDemo.insertMany([ {"InstructorName": "John Smith"}, {"InstructorName": "Sam Williams"}, {"InstructorName": "David Miller"} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5cd6804f7924bb85b3f48950"), ...

Read More

How to remove white spaces (leading and trailing) from string value in MongoDB?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 730 Views

To remove white spaces (leading and trailing) from string values in MongoDB, you can use the forEach() method combined with JavaScript's trim() function to iterate through documents and update them. Syntax db.collection.find().forEach(function(doc) { doc.fieldName = doc.fieldName.trim(); db.collection.update( { "_id": doc._id }, { "$set": { "fieldName": doc.fieldName } } ); }); Sample Data Let us first create a collection with documents containing white spaces ? ...

Read More

How to add a field with static value to MongoDB find query?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 689 Views

To add a field with static value to MongoDB find query, use the $literal operator with the aggregation framework. The $literal operator returns a value without parsing, making it perfect for adding constant values to query results. Syntax db.collection.aggregate([ { $project: { field1: 1, field2: 1, "staticFieldName": { $literal: ...

Read More
Showing 23501–23510 of 61,298 articles
Advertisements