Articles on Trending Technologies

Technical articles with clear explanations and examples

How can I use 'Not Like' operator in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 1K+ Views

In MongoDB, there is no direct 'NOT LIKE' operator as found in SQL. Instead, use the $not operator combined with regular expressions to achieve similar functionality for pattern matching exclusion. Syntax db.collection.find({ fieldName: { $not: /pattern/ } }); Sample Data db.notLikeOperatorDemo.insertMany([ { "StudentName": "John Doe" }, { "StudentName": "John Smith" }, { "StudentName": "John Taylor" }, { "StudentName": "Carol Taylor" }, { "StudentName": "David Miller" } ]); ...

Read More

Which characters are NOT allowed in MongoDB field names?

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

MongoDB field names have specific restrictions. You cannot use the $ symbol at the beginning of field names or the period (.) character anywhere in field names, as these are reserved for MongoDB's internal operations and dot notation. Syntax // Invalid field names { "$fieldName": "value" } // Cannot start with $ { "field.name": "value" } // Cannot contain dots { "field$name": "value" } // $ allowed in middle/end // Valid field names { "fieldName": "value" } { "field_name": "value" } { ...

Read More

Difference between count() and find().count() in MongoDB?

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

In MongoDB, count() and find().count() both return the number of documents in a collection, but they have subtle differences in how they handle query conditions and performance characteristics. Syntax db.collection.count(query, options) db.collection.find(query).count() Sample Data db.countDemo.insertMany([ {"UserId": 1, "UserName": "John"}, {"UserId": 2, "UserName": "Carol"}, {"UserId": 3, "UserName": "Bob"}, {"UserId": 4, "UserName": "Mike"} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5c7f9d278d10a061296a3c5d"), ...

Read More

Querying an array of arrays in MongoDB?

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

Use the $elemMatch operator with nested $in to query an array of arrays in MongoDB. This approach allows you to search for specific values within nested array structures. Syntax db.collection.find({ "arrayField": { $elemMatch: { $elemMatch: { $in: ["searchValue"] } ...

Read More

Matching an array field that contains any combination of the provided array in MongoDB?

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

To match documents where an array field contains any combination of the provided array values in MongoDB, use the $not operator combined with $elemMatch and $nin. This approach finds documents where the array contains only elements from the specified set. Syntax db.collection.find({ arrayField: { $not: { $elemMatch: { $nin: ["value1", "value2", "value3"] } } } }); Sample Data ...

Read More

How to efficiently perform "distinct" with multiple keys in MongoDB?

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

To efficiently perform distinct operations with multiple keys in MongoDB, use the aggregation framework with the $group stage. This groups documents by multiple fields and returns unique combinations. Syntax db.collection.aggregate([ { $group: { _id: { field1: "$field1", field2: "$field2", ...

Read More

How do I make case-insensitive queries on MongoDB?

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

To make case-insensitive queries in MongoDB, use regular expressions with the i flag or the $regex operator. This allows you to match documents regardless of letter case variations. Syntax // Method 1: Regular Expression with i flag db.collection.find({ "field": /pattern/i }); // Method 2: $regex operator db.collection.find({ "field": { $regex: "pattern", $options: "i" } }); Sample Data db.caseInsensitiveDemo.insertMany([ {"UserName": "David"}, {"UserName": "DAVID"}, {"UserName": "david"}, {"UserName": "Carol"}, {"UserName": "Mike"}, ...

Read More

How to remove an element from a doubly-nested array in a MongoDB document?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 2K+ Views

To remove an element from a doubly-nested array in MongoDB document, you can use $pull operator with proper dot notation to target the nested array location. Syntax db.collection.update( { "queryCondition": "value" }, { $pull: { "outerArray.index.innerArray": { "field": "valueToRemove" } } } ); Sample Data To understand the concept, let us create a collection with sample documents ? db.removeElementFromDoublyNestedArrayDemo.insertMany([ { "_id": "1", "UserName": "Larry", ...

Read More

Include all existing fields and add new fields to document in MongoDB?

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

To include all existing fields and add new fields to a document in MongoDB, use the $addFields operator in an aggregation pipeline. This operator preserves all existing fields while adding new computed fields to the output. Syntax db.collection.aggregate([ { $addFields: { "newField1": "value1", "newField2": "$existingField", "newField3": { $multiply: ["$field1", ...

Read More

Find all duplicate documents in a MongoDB collection by a key field?

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

To find all duplicate documents in a MongoDB collection by a key field, use the aggregation framework with $group and $match stages to group by the field and filter groups with count greater than 1. Syntax db.collection.aggregate([ { $group: { _id: { fieldName: "$fieldName" }, documents: { $addToSet: "$_id" }, count: { $sum: 1 } }}, { $match: { count: { ...

Read More
Showing 23811–23820 of 61,297 articles
Advertisements