Database Articles

Page 94 of 547

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

How to find a record by _id in MongoDB?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 731 Views

To find a record by _id in MongoDB, use the find() method with the ObjectId as the query criteria. Every document in MongoDB has a unique _id field that serves as the primary key. Syntax db.collectionName.find({"_id": ObjectId("your_object_id")}); Sample Data Let us create a collection with sample customer documents ? db.findRecordByIdDemo.insertMany([ {"CustomerName": "Larry", "CustomerAge": 26}, {"CustomerName": "Bob", "CustomerAge": 20}, {"CustomerName": "Carol", "CustomerAge": 22}, {"CustomerName": "David", "CustomerAge": 24} ]); { "acknowledged": ...

Read More

How to improve querying field in MongoDB?

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

To improve querying performance in MongoDB, you need to use indexes. Indexes create efficient data structures that allow MongoDB to quickly locate documents without scanning the entire collection. Syntax db.collection.createIndex({ "fieldName": 1 }); db.collection.find({ "fieldName": "searchValue" }); Sample Data Let us create a collection with documents ? db.improveQueryDemo.insertOne({ "PlayerDetails": [ {"PlayerName": "John", "PlayerGameScore": 5690}, {"PlayerName": "Carol", "PlayerGameScore": 2690} ] }); { ...

Read More

MongoDB equivalent of SELECT field AS `anothername`?

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

In MySQL, we give an alias name for a column. Similarly, you can give an alias name for field name in MongoDB using the $project stage in aggregation pipeline. Syntax db.collectionName.aggregate([ { "$project": { "_id": 0, "aliasName": "$originalFieldName" }} ]); Sample Data Let us first create a collection with documents ? db.selectFieldAsAnotherNameDemo.insertMany([ {"Name": "Larry"}, {"Name": "Robert"}, ...

Read More

How to use $slice operator to get last element of array in MongoDB?

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

To get the last element of an array in MongoDB, use the $slice operator with -1 in the projection. This returns only the last element of the specified array field. Syntax db.collectionName.find( {}, { arrayFieldName: { $slice: -1 } } ); Sample Data Let us create a collection with sample student documents ? db.getLastElementOfArrayDemo.insertMany([ { "StudentName": "James", "StudentMathScore": [78, 68, 98] ...

Read More
Showing 931–940 of 5,468 articles
« Prev 1 92 93 94 95 96 547 Next »
Advertisements