Articles on Trending Technologies

Technical articles with clear explanations and examples

How to get values greater than a specific value from an embedded list in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 400 Views

To get values greater than a specific value from an embedded list in MongoDB, use the $gt operator with dot notation to target fields within the embedded array. This will return entire documents that contain at least one array element matching the condition. Syntax db.collection.find({ "arrayField.field": { $gt: value } }) Sample Data Let us create a collection with documents containing embedded arrays ? db.demo317.insertMany([ { "id": 101, "details": [ ...

Read More

How to restrict inserting an item with the same name in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 235 Views

To restrict inserting items with the same name in MongoDB, create a unique index on the field using createIndex() with unique: true. This prevents duplicate values and throws an error when attempting to insert duplicates. Syntax db.collection.createIndex({"fieldName": 1}, {unique: true}); Example Create a unique index on the SubjectName field to prevent duplicate subject names ? db.demo316.createIndex({"SubjectName": 1}, {unique: true}); { "createdCollectionAutomatically": true, "numIndexesBefore": 1, "numIndexesAfter": 2, "ok": 1 } Insert Unique ...

Read More

Updating nested document in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 377 Views

To update nested documents in MongoDB, use the $set operator with dot notation to specify the exact path to the field you want to modify. This allows you to update deeply nested fields without affecting other parts of the document. Syntax db.collection.update( { "matchField": "value" }, { $set: { "path.to.nested.field": "newValue" } } ); Sample Data Let us create a collection with a nested document ? db.demo315.insertOne({ _id: 101, details: [ ...

Read More

MongoDB query to set a sub item in an array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 321 Views

To set a sub item in an array in MongoDB, use the $ positional operator which identifies the first array element that matches the query condition and allows you to update specific fields within that element. Syntax db.collection.update( {"arrayField.subField": "matchValue"}, {$set: {"arrayField.$.subField": "newValue"}} ); Sample Data Let us create a collection with product documents ? db.demo22.insertOne({ ProductId: 101, ProductDetails: [ { ...

Read More

How can I update child objects in MongoDB database?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 373 Views

To update child objects in MongoDB, use the $set operator with dot notation to target specific fields within nested documents. This allows you to modify individual properties of embedded objects without affecting other fields. Syntax db.collection.update( { "matchField": "value" }, { $set: { "parentObject.childField": "newValue" } } ); Sample Data Let us first create a collection with documents ? db.demo21.insertOne({ "StudentId": "STU-101", "StudentDetails": { "StudentName": "Chris", ...

Read More

Shutdown MongoDB with auth enabled?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 175 Views

To shutdown MongoDB with authentication enabled, you need to be authenticated as an administrative user and use the shutdownServer() method on the admin database. Syntax use admin db.auth("username", "password") db.shutdownServer() Method 1: Shutdown with Authentication First, connect to the admin database and authenticate with administrative credentials ? use admin switched to db admin Authenticate as an admin user with shutdown privileges ? db.auth("admin", "password") 1 Now shutdown the MongoDB server ? db.shutdownServer() server should be ...

Read More

Get the number of open connections in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 469 Views

To get the number of open connections in MongoDB, use the serverStatus() method which returns comprehensive server statistics including connection information. Syntax db.serverStatus() Example Execute the following command to retrieve server status ? db.serverStatus() This will produce output with various server metrics. Look for the connections section ? { "host" : "DESKTOP-QN2RB3H", "version" : "4.0.5", "process" : "mongod", "connections" : { "current" : 1, ...

Read More

Using a regex with text search in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 578 Views

To filter records using Regular Expression in MongoDB, use the $regex operator. This allows you to search for documents that match specific text patterns within string fields. Syntax db.collection.find({ "fieldName": { $regex: /pattern/, $options: "flags" } }); Sample Data Let's create a collection with sample documents ? db.demo19.insertMany([ {"Values": "4321GH"}, {"Values": "12321_Carol"}, {"Values": "8765Mike"} ]); ...

Read More

Filter documents in MongoDB if all keys exist as fields?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 350 Views

To filter documents in MongoDB where all specified keys exist as fields in an array, use the $all operator. This operator matches documents containing all specified elements in the array, regardless of order. Syntax db.collection.find({ "arrayField": { "$all": ["value1", "value2", "value3"] } }); Sample Data db.demo17.insertMany([ {"ListOfSubject": ["MySQL", "MongoDB", "Java"]}, {"ListOfSubject": ["C", "Python", "Java"]}, {"ListOfSubject": ["C++", "MongoDB", "PL/SQL"]} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

Perform group and distinct together in a single MongoDB query

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 545 Views

To perform group and distinct operations together in MongoDB, use the $group aggregation stage with $addToSet operator. This approach groups documents while collecting distinct values from specified fields in a single query. Syntax db.collection.aggregate([ { $group: { _id: null, distinctField1: { $addToSet: "$fieldName1" }, distinctField2: { $addToSet: "$fieldName2" }, ...

Read More
Showing 23171–23180 of 61,297 articles
Advertisements