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
Articles on Trending Technologies
Technical articles with clear explanations and examples
MongoDB query to get distinct FirstName values from documents
To get distinct FirstName values from documents in MongoDB, use the distinct() method. This method returns an array of unique values for the specified field across all documents in the collection. Syntax db.collection.distinct("fieldName") Sample Data db.demo303.insertMany([ {FirstName:"Chris", LastName:"Brown"}, {FirstName:"John", LastName:"Doe"}, {FirstName:"Chris", LastName:"Smith"}, {FirstName:"John", LastName:"Smith"}, {FirstName:"David", LastName:"Miller"} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5e4ea0f6f8647eb59e56202f"), ...
Read MoreField selection within MongoDB query using dot notation?
Field selection in MongoDB using dot notation allows you to access and filter nested fields within embedded documents or arrays. Use dot notation to specify which fields to include or exclude in query results. Syntax db.collection.find( {"parentField.nestedField": "value"}, {"parentField.specificField": 1/0} ); Sample Data db.demo302.insertMany([ {"Id": 101, "details": [{"Name": "Chris", "Age": 21, "Subject": "MySQL"}]}, {"Id": 102, "details": [{"Name": "Bob", "Age": 23, "Subject": "MongoDB"}]}, {"Id": 103, "details": [{"Name": "David", "Age": 20, "Subject": "Java"}]} ]); ...
Read MoreMongoDB query to change order of array elements?
To change the order of array elements in MongoDB, you can use JavaScript operations with the forEach() method to swap elements or use aggregation pipeline with array operators for more complex reordering. Syntax // Method 1: Using forEach with JavaScript swap db.collection.find().forEach(function(doc) { // Swap elements using temporary variable var temp = doc.arrayField[index1]; doc.arrayField[index1] = doc.arrayField[index2]; doc.arrayField[index2] = temp; db.collection.update({_id: doc._id}, {$set: {arrayField: doc.arrayField}}); }); // Method 2: Using $set with explicit array db.collection.update( ...
Read MoreMongoDB query to access an object in an array
To access an object in an array in MongoDB, use dot notation to specify the field path. This allows you to query nested objects within arrays and retrieve documents that match specific criteria. Syntax db.collection.find({"arrayField.objectField": "value"}) Sample Data db.demo299.insertMany([ { "id": 100, "Name": "Robert", "details": [ { ...
Read MoreMongoDB transaction & indexes for duplicate values
To prevent duplicate values within array fields in MongoDB, use createIndex() with the unique option. When a unique index is applied to an array field, MongoDB treats each array element as a separate index entry, preventing any value from appearing in multiple documents. Syntax db.collection.createIndex( { "arrayField": 1 }, { "unique": true } ); Sample Data Let us create a collection with documents ? db.demo298.insertMany([ { Name: "Chris", Marks: [46, 79] }, { Name: "David", Marks: ...
Read MoreMongoDB query to insert but limit the total records
To insert documents while limiting the total records in a MongoDB collection, use capped collections with the capped: true option and set the max parameter to define the maximum number of documents. Syntax db.createCollection("collectionName", { capped: true, size: sizeInBytes, max: maxDocuments }); Create Capped Collection Let us create a capped collection that allows a maximum of 4 documents ? db.createCollection("demo297", {capped: true, size: 4, max: 4}); { "ok" : 1 } Insert Documents Now ...
Read MoreNative Querying MongoDB inside array and get the count
To query inside array and check for existence to get the count, use $exists. This operator checks whether a specific field exists within array elements and returns documents that match the condition. Syntax db.collection.count({ "arrayField.nestedField": { $exists: true } }); Sample Data db.demo296.insertMany([ { "id": 101, "Name": "Chris", "details": [ ...
Read MoreQuerying object's field array values in MongoDB?
To query object's field array values in MongoDB, use the field name directly with the array element value. MongoDB automatically searches within array fields to find documents containing the specified value. Syntax db.collection.find({arrayFieldName: "value"}); Sample Data db.demo295.insertMany([ {"status": ["Active", "Inactive"]}, {"status": ["Yes", "No"]} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5e4d4ea65d93261e4bc9ea39"), ObjectId("5e4d4eb15d93261e4bc9ea3a") ] } ...
Read MoreMongoDB: combining AND and OR?
In MongoDB, you can combine AND and OR operators to create complex query conditions. Use $and to ensure all conditions are met, and $or to match any one of multiple conditions within the same query. Syntax db.collection.find({ $and: [ { $or: [ { "field1": "value1" }, ...
Read MoreMongoDB - How to check for equality in collection and in embedded document?
To check for equality between a field in the main collection and a field in an embedded document, use the $where operator with JavaScript expressions. This allows you to compare values across different nesting levels within the same document. Syntax db.collection.find({ $where: 'this.fieldName === this.embeddedDoc.fieldName' }); Sample Data db.demo292.insertMany([ { "FirstName": "Chris", "LastName": "Brown", "Friend": { ...
Read More