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
Articles by Chandu yadav
Page 28 of 81
Get the first element in an array and return using MongoDB Aggregate?
To get the first element from an array using MongoDB aggregation, use the $arrayElemAt operator or combine $unwind with $first. The $arrayElemAt method is simpler and more efficient for this task. Syntax db.collection.aggregate([ { $project: { "fieldName": { $arrayElemAt: ["$arrayField", 0] } } } ]); Sample Data db.getFirstElementInArrayDemo.insertMany([ { ...
Read MoreHow to reduce MongoDB storage space after deleting large amount of data?
To reduce MongoDB storage space after deleting a large amount of data, you need to reclaim the freed disk space since MongoDB doesn't automatically release space after deletions. The database continues to use the same amount of disk space even after documents are removed. Syntax db.runCommand({ "compact": "collectionName" }) Or for the entire database: db.repairDatabase() Method 1: Using compact Command (Recommended) The compact command is the modern approach to reclaim space from a specific collection ? db.runCommand({ "compact": "users" }) { "ok" : 1, "bytesFreed" ...
Read MoreIs it possible to sum two fields in MongoDB using the Aggregation framework?
Yes, it is possible to sum two fields in MongoDB using the $add operator within the $project stage of the aggregation framework. This creates a new computed field containing the sum of existing fields. Syntax db.collection.aggregate([ { $project: { field1: "$field1", field2: "$field2", sumField: { $add: ["$field1", "$field2"] } ...
Read MorePerform conditional upserts or updates in MongoDB
In MongoDB, you can perform conditional updates that only modify a field if certain conditions are met. The $max operator is particularly useful for conditional updates - it only updates a field if the new value is greater than the current value. Syntax db.collection.update( { "field": "matchValue" }, { $max: { "fieldToUpdate": newValue } } ); Sample Data Let us create a collection with sample student score documents ? db.conditionalUpdatesDemo.insertMany([ { "_id": ...
Read MoreOnly insert if a value is unique in MongoDB else update
To only insert if a value is unique in MongoDB (or update if it exists), use the upsert option with the update() method. When upsert: true is specified, MongoDB performs an update if the document matches the query criteria, or inserts a new document if no match is found. Syntax db.collection.update( { field: "matchValue" }, { $set: { field: "newValue" } }, { upsert: true } ); Sample Data db.onlyInsertIfValueIsUniqueDemo.insertMany([ {"StudentName": "Larry", "StudentAge": 22}, ...
Read MoreQuery MongoDB with length criteria?
To query MongoDB with length criteria, you can use the $regex operator with regular expressions to match string fields based on their character length. Syntax db.collection.find({ "fieldName": { $regex: /^.{minLength, maxLength}$/ } }); Where minLength and maxLength define the character count range. Sample Data db.queryLengthDemo.insertMany([ {"StudentFullName": "John Smith"}, {"StudentFullName": "John Doe"}, {"StudentFullName": "David Miller"}, {"StudentFullName": "Robert Taylor"}, {"StudentFullName": "Chris Williams"} ]); { ...
Read MoreAccess objects from the nested objects structure in MongoDB
To access objects from nested objects structure in MongoDB, use dot notation to traverse through multiple levels of nested documents. This allows you to query and retrieve documents based on deeply nested field values. Syntax db.collection.find({ "parentObject.childObject.nestedField": "value" }); Create Sample Data Let us first create a collection with nested documents ? db.nestedObjectDemo.insertOne({ "Student": { "StudentDetails": { "StudentPersonalDetails": { ...
Read MoreDrop all indexes from all the collections in a MongoDB database using the command line?
To drop all indexes from all collections in a MongoDB database, use the dropIndexes command with the forEach method to iterate through all collections. This operation removes all custom indexes while preserving the mandatory _id index. Syntax db.getCollectionNames().forEach(function(collectionName) { db.runCommand({dropIndexes: collectionName, index: "*"}); }); Example Let's first check the current database and view existing indexes before dropping them ? db Test View Current Indexes Check existing indexes in a collection ? db.indexingDemo.getIndexes(); [ ...
Read MoreSearch for a text in MongoDBs Double Nested Array?
To search for a text in MongoDB's double nested array, use dot notation to traverse through multiple array levels. The dot notation format is parentArray.childArray.fieldName to target specific fields deep within nested structures. Syntax db.collection.find({ "parentArray.childArray.fieldName": "searchValue" }); Sample Data Let us create a collection with double nested array documents ? db.doubleNestedArrayDemo.insertMany([ { "StudentId": "1000", "StudentName": "Larry", "StudentDetails": [ ...
Read MoreHow to get all the collections from all the MongoDB databases?
To get all the collections from all MongoDB databases, you need to first retrieve all databases and then iterate through each database to get their collections. Syntax // Step 1: Get all databases switchDatabaseAdmin = db.getSiblingDB("admin"); allDatabaseName = switchDatabaseAdmin.runCommand({ "listDatabases": 1 }).databases; // Step 2: Iterate through databases to get collections allDatabaseName.forEach(function(databaseName) { db = db.getSiblingDB(databaseName.name); collectionName = db.getCollectionNames(); // Process collections as needed }); Method 1: Get All Database Names First, let us get all the databases using the admin ...
Read More