Articles on Trending Technologies

Technical articles with clear explanations and examples

MongoDB Indexes - Is it possible to create both normal & compound at the same time?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 140 Views

Yes, you can create both normal (single field) and compound (multiple field) indexes simultaneously in MongoDB using the createIndex() method. MongoDB provides complete support for indexes on any field in a collection of documents. Syntax db.collection.createIndex({field1: 1, field2: 1, field3: 1}); Where 1 indicates ascending order and -1 indicates descending order. Example Let us create a compound index on multiple fields and insert sample documents ? db.demo622.createIndex({_id: 1, Name: 1, Age: 1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, ...

Read More

MongoDB Aggregate Second Element from Input Element?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 163 Views

To aggregate second element from input element in MongoDB, use mapReduce() with a counter variable in the scope. This method allows you to skip elements and select specific positions from a collection based on a divisor pattern. Syntax db.collection.mapReduce( function() { track++; if (track % div == 0) { emit(key, value); } }, ...

Read More

Aggregate by country, state and city in a MongoDB collection with multiple documents

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

Aggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result. To aggregate by country, state and city in MongoDB, use the $group stage with $addToSet to organize data hierarchically. Syntax db.collection.aggregate([ { "$group": { "_id": { "Country": "$Country", "state": "$state" }, "City": { "$addToSet": { "City": "$City" ...

Read More

MongoDB – Fix "Failed to convert from type String to type Date"?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 559 Views

To fix the "Failed to convert from type String to type Date" error in MongoDB, use $dateFromString in an aggregation pipeline. This operator converts a date/time string to a proper Date object that MongoDB can process. Syntax db.collection.aggregate([ { $project: { fieldName: { $dateFromString: { ...

Read More

Can we work MongoDB findOne() with long type _id?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 211 Views

Yes, MongoDB's findOne() method works with NumberLong data type for _id fields. Use NumberLong() to wrap long integer values when storing and querying documents with numeric _id values that exceed JavaScript's safe integer range. Syntax db.collection.findOne({_id: NumberLong("longValue")}); Create Sample Data Let's create a collection with NumberLong _id values − db.demo618.insertMany([ {_id: NumberLong("6336366454"), Name: "Chris"}, {_id: NumberLong("6336366455"), Name: "David"}, {_id: NumberLong("6336366456"), Name: "Bob"} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

Group query upon nested object in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 354 Views

To group documents by fields within nested objects in MongoDB, use dot notation with the $group aggregation stage. The dot notation allows you to access nested fields using the format "parentField.nestedField". Syntax db.collection.aggregate([ { $group: { _id: "$parentField.nestedField", count: { $sum: 1 } } ...

Read More

How can I aggregate collection and group by field count in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 733 Views

In MongoDB, use the $group stage with aggregate() to group documents by field values and count occurrences. The $sum operator with value 1 counts documents in each group. Syntax db.collection.aggregate([ { $group: { _id: "$fieldName", count: { $sum: 1 } } } ]); Sample Data ...

Read More

How to use $type in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 174 Views

The $type operator in MongoDB selects documents where the value of a specified field matches a particular BSON type. This is useful for filtering documents based on data types rather than values. Syntax db.collection.find({ "field": { $type: "bsonType" } }) Where bsonType can be a string name (like "string", "number", "object") or numeric BSON type code. Sample Data Let's create a collection with mixed data types ? db.demo615.insertMany([ { "Value": 100 }, { "Value": "100" }, { "Value": "300" ...

Read More

What is ({$natural: 1}) in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

The {$natural: 1} is a MongoDB sort option that returns documents in their natural insertion order. When used with {$natural: -1}, it works like LIFO (Last In First Out), showing the most recently inserted document first. Syntax db.collection.find().sort({$natural: 1}); // Insertion order db.collection.find().sort({$natural: -1}); // Reverse insertion order Sample Data Let us create a collection with documents ? db.demo614.insertMany([ {"CountryName": "US"}, {"CountryName": "UK"}, {"CountryName": "AUS"}, {"CountryName": "IND"} ]); { ...

Read More

Implementing String Comparison in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 832 Views

To implement string comparison in MongoDB, use the $strcasecmp operator in aggregation pipelines. It performs case-insensitive comparison of two strings and returns: 1 if first string is "greater than" the second string. 0 if the two strings are equal. -1 if the first string is "less than" the second string. Syntax { $project: { fieldName1: 1, fieldName2: 1, comparisonResult: { $strcasecmp: ["$field1", "$field2"] } ...

Read More
Showing 22981–22990 of 61,297 articles
Advertisements