Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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?
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 MoreHow to restrict inserting an item with the same name in MongoDB?
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 MoreUpdating nested document in MongoDB
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 MoreMongoDB query to set a sub item in an array?
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 MoreHow can I update child objects in MongoDB database?
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 MoreShutdown MongoDB with auth enabled?
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 MoreGet the number of open connections in MongoDB?
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 MoreUsing a regex with text search in MongoDB
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 MoreFilter documents in MongoDB if all keys exist as fields?
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 MorePerform group and distinct together in a single MongoDB query
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