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
Database Articles
Page 94 of 547
How to return only value of a field in MongoDB?
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 MoreWrite a MongoDB query to get nested value?
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 MoreGet component of Date / ISODate in MongoDB?
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 MoreFind the document by field name with a specific value in MongoDB?
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 MoreCheck if MongoDB database exists?
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 MoreGet MongoDB Databases in a JavaScript Array?
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 MoreHow to find a record by _id in MongoDB?
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 MoreHow to improve querying field in MongoDB?
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 MoreMongoDB equivalent of SELECT field AS `anothername`?
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 MoreHow to use $slice operator to get last element of array in MongoDB?
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