MongoDB Articles

Page 42 of 111

Get the number of open connections in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 463 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 572 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 344 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 539 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

MongoDB: How to query a collection named "version"?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 179 Views

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 More

How to use collMod in MongoDB runCommand()?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 854 Views

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 More

Query MongoDB for a nested search

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 291 Views

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

MongoDB query to insert an array element with a condition?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 734 Views

To insert an array element with a condition in MongoDB, use the $push operator combined with the $ positional operator to add new elements to nested arrays when specific conditions are met. Syntax db.collection.update( {"arrayField.conditionField": "value"}, {$push: {"arrayField.$.targetArray": newElement}} ); Sample Data db.demo11.insertOne({ "ListOfStudent": [ { "StudentName": "Chris", "ListOfScore": ...

Read More

MongoDB query for a single field

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 819 Views

To query a single field in MongoDB, use the find() method with projection. Projection allows you to specify which fields to include or exclude from the query results. Syntax db.collection.find({}, { "fieldName": 1, "_id": 0 }); Sample Data Let us first create a collection with documents ? db.demo10.insertMany([ { "StudentId": 101, "StudentName": "Chris" }, { "StudentId": 102, "StudentName": "David" }, { "StudentId": 103, "StudentName": "Bob" } ]); { "acknowledged": true, ...

Read More

How to get a "-Infinity" result for $avg in an aggregate query?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 188 Views

In MongoDB aggregation, the $avg operator returns -Infinity when calculating the average of values that include -Infinity. This occurs because any arithmetic operation involving -Infinity propagates the infinite value. Syntax db.collection.aggregate([ { $group: { "_id": "$groupField", "average": { $avg: "$numericField" } } } ]); Sample Data ...

Read More
Showing 411–420 of 1,106 articles
« Prev 1 40 41 42 43 44 111 Next »
Advertisements