MongoDB Articles

Page 91 of 111

How to get documents by tags in MongoDB?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 586 Views

To find documents by tags in MongoDB, you can use the $elemMatch operator or simple array matching. MongoDB provides several ways to query documents containing specific tags in array fields. Syntax // Using $elemMatch operator db.collection.find({Tags: { $elemMatch: { $eq: "tagValue" } }}); // Simple array matching db.collection.find({Tags: "tagValue"}); // Multiple tags using $in db.collection.find({Tags: { $in: ["tag1", "tag2"] }}); Sample Data db.getDocumentsByTagsDemo.insertMany([ {"Tags": ["Tag-1", "Tag-2", "Tag-3"]}, {"Tags": ["Tag-2", "Tag-4", "Tag-5"]}, {"Tags": ["Tag-6", "Tag-4", "Tag-3"]} ]); ...

Read More

How to get connected clients in MongoDB?

George John
George John
Updated on 15-Mar-2026 903 Views

To get connected clients in MongoDB, use the db.currentOp() method with true parameter to display all operations including idle connections. The client field in the output shows the IP address and port of each connected client. Syntax db.currentOp(true) Example Run the following command to view all connected clients ? db.currentOp(true) The output displays all connected clients with detailed information ? { "inprog" : [ { "host" : "DESKTOP-QN2RB3H:27017", ...

Read More

How do you limit an array sub-element in MongoDB?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 857 Views

You can use $slice operator to limit array elements in MongoDB query results. The $slice operator controls how many elements from an array field are returned in the projection. Syntax db.collection.find( {}, { "arrayField": { $slice: N } } ) Where N can be: Positive number: Returns first N elements Negative number: Returns last N elements [skip, limit]: Skips elements and limits result Sample Data db.limitAnArrayDemo.insertOne({ _id: 101, "PlayerName": "Bob", ...

Read More

Update only specific fields in MongoDB?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 873 Views

To update only specific fields in MongoDB, use the $set operator. This operator modifies the value of a field without affecting other fields in the document. Syntax db.collection.update( { "field": "matchValue" }, { $set: { "fieldToUpdate": "newValue" } } ); Sample Data db.updateOnlySpecificFieldDemo.insertMany([ { "EmployeeName": "John", "EmployeeCountryName": "UK" }, { "EmployeeName": "Larry", "EmployeeCountryName": "US" }, { "EmployeeName": "David", "EmployeeCountryName": "AUS" } ]); { "acknowledged": true, ...

Read More

How to return only value of a field in MongoDB?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 1K+ Views

To return only the values of a field in MongoDB, you can use projection with find() to select specific fields, or use forEach with an array to extract field values into a simple array format. Syntax // Method 1: Using projection db.collection.find({}, { "fieldName": 1, "_id": 0 }); // Method 2: Using forEach to extract values var output = []; db.collection.find().forEach(function(doc) { output.push(doc.fieldName); }); Sample Data db.returnOnlyValueOfFieldDemo.insertMany([ { "ClientName": "Larry" }, { "ClientName": "Chris" }, ...

Read More

Write a MongoDB query to get nested value?

George John
George John
Updated on 15-Mar-2026 277 Views

You can use dot notation to query nested values in MongoDB documents. Dot notation allows you to access fields within embedded documents using the format parentField.nestedField. Syntax db.collection.find({"parentField.nestedField": "value"}); Sample Data Let us first create a collection with nested documents ? db.nestedQueryDemo.insertMany([ { "EmployeeName": "John", "EmployeeDetails": { "_id": "EMP-101", ...

Read More

Get component of Date / ISODate in MongoDB?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 286 Views

To get component of Date/ISODate in MongoDB, you can access individual date components like year, month, day, hour, minute, and second using JavaScript date methods on the ISODate field. Syntax // Access date field var dateField = db.collection.findOne().dateFieldName; // Get components dateField.getFullYear() // Year (e.g., 2019) dateField.getMonth() // Month (0-11, January=0) dateField.getDate() // Day of month (1-31) dateField.getHours() // Hour (0-23) dateField.getMinutes() // Minutes (0-59) dateField.getSeconds() ...

Read More

Find the document by field name with a specific value in MongoDB?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 689 Views

To find documents by field name with a specific value in MongoDB, you can use the $exists operator to check if a field exists, or use dot notation to match specific field values in nested documents. Syntax // Check if field exists db.collection.find({"fieldName": {$exists: true}}) // Find by specific value in nested field db.collection.find({"parent.child": "specificValue"}) Sample Data db.findByFieldName.insertMany([ { "Client": { "ClientDetails": { ...

Read More

Check if MongoDB database exists?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 3K+ Views

To check if a MongoDB database exists, use the indexOf() method on the list of database names. This returns the index position if the database exists, or -1 if it doesn't exist. Syntax db.getMongo().getDBNames().indexOf("databaseName"); Return Values Positive number (0 or greater): Database exists at that index position -1: Database does not exist Case 1: Database Exists Check if the "test" database exists ? db.getMongo().getDBNames().indexOf("test"); 6 The result 6 means the "test" database exists and is located at index 6 in the database list. ...

Read More

Get MongoDB Databases in a JavaScript Array?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 294 Views

To get MongoDB databases in a JavaScript array, you can use the listDatabases command with runCommand() and then extract database names into an array using JavaScript. Syntax use admin; allDatabasesDetails = db.runCommand({listDatabases: 1}); databaseNames = allDatabasesDetails.databases.map(db => db.name); Step 1: Get All Databases Details First, switch to the admin database and run the listDatabases command ? use admin; allDatabasesDetails = db.runCommand({listDatabases: 1}); { "databases" : [ { "name" : "admin", ...

Read More
Showing 901–910 of 1,106 articles
« Prev 1 89 90 91 92 93 111 Next »
Advertisements