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
Big Data Analytics Articles
Page 41 of 135
MongoDB 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 MoreMongoDB: How to query a collection named "version"?
To query a collection named "version" in MongoDB, use the getCollection() method with the collection name as a string parameter. This is necessary because "version" is a reserved word in MongoDB shell. Syntax db.getCollection("version").find(); db.getCollection("version").insertOne({document}); Create the Collection First, create a collection named "version" ? db.createCollection("version"); { "ok" : 1 } Insert Sample Data Insert multiple version documents using insertMany() ? db.getCollection("version").insertMany([ {"VersionName": "1.0"}, {"VersionName": "1.1"}, {"VersionName": "1.2"} ]); ...
Read MoreHow to use collMod in MongoDB runCommand()?
The collMod command allows you to modify options for an existing collection or update view definitions in MongoDB. You can execute this command using runCommand() to change collection behavior and properties. Syntax db.runCommand({ collMod: "collectionName", option: value }); Sample Data Let us first create a collection with sample documents ? db.demo13.insertMany([ {"StudentFirstName": "Chris"}, {"StudentFirstName": "David"}, {"StudentFirstName": "Bob"} ]); { "acknowledged": true, ...
Read MoreQuery MongoDB for a nested search
To perform nested searches in MongoDB, use the $and operator combined with $or to create complex query conditions. This allows you to match documents that satisfy multiple criteria with optional alternative conditions. Syntax db.collection.find({ $and: [ { field1: "value1" }, { field2: "value2" }, { $or: [ { field3: "option1" }, ...
Read More