To find the intersection of sets between documents in a single MongoDB collection, use the $setIntersection operator within an aggregation pipeline. This operator returns an array containing elements that appear in all specified arrays. Syntax { $project: { "result": { $setIntersection: ["$array1", "$array2", "$array3", ...] } } } Sample Data db.setInterSectionDemo.insertMany([ {"_id": 101, ... Read More
To find the datatype of all fields in MongoDB, use the typeof operator with findOne() to check individual field types, or use aggregation pipeline to analyze multiple fields systematically. Syntax typeof db.collectionName.findOne().fieldName; Sample Data Let us first create a collection with documents ? db.findDataTypeDemo.insertOne({ "ClientName": "Chris", "isMarried": false, "age": 25, "salary": 50000.50 }); { "acknowledged": true, "insertedId": ObjectId("5ccf2064dceb9a92e6aa1952") } Display the document to ... Read More
To perform alphanumeric sort in MongoDB, use the collation method with numericOrdering: true. This ensures that numeric portions within strings are sorted numerically rather than lexically (e.g., "STU101" comes before "STU1010"). Syntax db.collection.find().sort({field: 1}).collation({ locale: "en_US", numericOrdering: true }); Sample Data db.alphanumericSortDemo.insertMany([ {"StudentId": "STU1010"}, {"StudentId": "STU1101"}, {"StudentId": "STU1901"}, {"StudentId": "STU908"}, {"StudentId": "STU101"} ]); Display all documents to see the insertion order ? ... Read More
To reverse array field in MongoDB, you can use the forEach() method to iterate through documents and manually reorder array elements using the $set operator. Syntax db.collection.find().forEach(function (document) { var reversedArray = document.arrayField.reverse(); db.collection.update(document, { $set: {arrayField: reversedArray } }); }); Sample Data Let us first create a collection with documents ? db.reverseArrayDemo.insertOne({"Skills":["C", "Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ccddf99dceb9a92e6aa1946") } Following is the query to display all documents from ... Read More
To pull all elements from an array in MongoDB without any condition, use the $set operator to replace the entire array field with an empty array []. This effectively removes all elements from the specified array. Syntax db.collection.update( { "field": "value" }, { $set: { "arrayField": [] } } ); Sample Data Let us create a collection with documents containing arrays ? db.pullAllElementDemo.insertMany([ { "StudentId": 101, ... Read More
To update object at specific array index in MongoDB, use the $set operator with dot notation to target the exact position in nested arrays. Specify the array path with numeric indices to access objects at specific locations. Syntax db.collection.update( { "field": "value" }, { $set: { "arrayName.index1.index2.fieldName": "newValue" } } ); Sample Data Let us first create a collection with nested array structure: db.updateObjectDemo.insertOne({ id: 101, "StudentDetails": [ ... Read More
To increment a field in MongoDB, use the $inc operator with the update() method. The $inc operator increases the numeric value of a field by a specified amount. Syntax db.collection.update( { "query": "criteria" }, { $inc: { "fieldName": incrementValue } } ); Sample Data Let us create a collection with player scores ? db.incrementDemo.insertMany([ { "PlayerScore": 100 }, { "PlayerScore": 290 }, { "PlayerScore": 560 } ]); { ... Read More
MongoDB supports native boolean values true and false. You can query boolean fields using equality operators like $eq and $ne, or by direct value matching. Syntax // Direct boolean matching db.collection.find({"booleanField": true}); db.collection.find({"booleanField": false}); // Using $eq operator db.collection.find({"booleanField": {$eq: true}}); // Using $ne operator db.collection.find({"booleanField": {$ne: false}}); Sample Data Let us create a collection with boolean values − db.booleanQueryDemo.insertMany([ {"UserName": "John", "UserAge": 23, "isMarried": true}, {"UserName": "Chris", "UserAge": 21, "isMarried": false}, {"UserName": "Robert", "UserAge": 24, ... Read More
To remove and update existing records in MongoDB, use the $pull operator which removes elements from an array that match specified criteria. This operator effectively updates the document by removing the matching array elements. Syntax db.collection.update( { "filter_condition": "value" }, { "$pull": { "arrayField": { "field": "valueToRemove" } } } ); Sample Data Let us create a collection with documents containing user details in an array ? db.removeDemo.insertMany([ { "UserName": "Larry", ... Read More
To iterate a cursor and print documents in MongoDB, use the forEach() method with printjson. This approach allows you to process each document individually and format the output for better readability. Syntax db.collection.find().forEach(printjson); Create Sample Data Let's create a collection with student documents ? db.cursorDemo.insertMany([ {"StudentFullName": "John Smith", "StudentAge": 23}, {"StudentFullName": "John Doe", "StudentAge": 21}, {"StudentFullName": "Carol Taylor", "StudentAge": 20}, {"StudentFullName": "Chris Brown", "StudentAge": 24} ]); { "acknowledged": true, ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance