Articles on Trending Technologies

Technical articles with clear explanations and examples

Clearing items in a nested MongoDB array?

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

To clear items in a nested MongoDB array, use the $set operator to replace the entire array field with an empty array []. This removes all elements from the specified array field. Syntax db.collection.update( { "matchField": "value" }, { $set: { "arrayField": [] } } ); Sample Data Let us create a collection with nested arrays ? db.clearingItemsInNestedArrayDemo.insertOne({ "StudentName": "John", "StudentDetails": [ { ...

Read More

Search for a text in MongoDBs Double Nested Array?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 167 Views

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 More

Getting a distinct aggregation of an array field across indexes

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

To get a distinct aggregation of an array field across indexes in MongoDB, use the distinct() method on the array field. MongoDB will automatically utilize any existing index on that field to optimize the operation. Syntax db.collection.distinct("arrayFieldName") Sample Data Let's create a collection with documents containing array fields ? db.distinctAggregation.insertMany([ { "UserName": "Larry", "UserPost": ["Hi", "Hello"] }, { ...

Read More

Get only the FALSE value with MongoDB query

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

To get only the documents with FALSE values from a boolean field in MongoDB, use the find() method with a query condition that matches the false value directly or uses comparison operators. Syntax db.collection.find({ "fieldName": false }); // OR db.collection.find({ "fieldName": { "$ne": true } }); Sample Data Let us first create a collection with documents having an isEnable field with TRUE or FALSE values ? db.translateDefinitionDemo.insertMany([ { "_id": 10, "StudentName": "Larry", "isEnable": true }, { "_id": 20, "StudentName": "Chris", "isEnable": false }, ...

Read More

How to get all the collections from all the MongoDB databases?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 589 Views

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

Can I get the first item in a Cursor object in MongoDB?

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

Yes, you can get the first item in a cursor object in MongoDB using the findOne() method or by applying the limit(1) method to a cursor. Syntax // Method 1: Using findOne() db.collection.findOne(); db.collection.findOne({condition}); // Method 2: Using find().limit(1) db.collection.find().limit(1); Sample Data db.getFirstItemDemo.insertMany([ {"CustomerName": "Chris", "CustomerAge": 28}, {"CustomerName": "Larry", "CustomerAge": 26}, {"CustomerName": "Robert", "CustomerAge": 29}, {"CustomerName": "David", "CustomerAge": 39} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

MongoDB query to return only embedded document?

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

While MongoDB doesn't return only embedded documents directly, you can use projection to return specific fields and the $elemMatch operator to filter array elements that match certain conditions. Syntax db.collection.find( { "arrayField.nestedField": { $gte: value } }, { "arrayField": { $elemMatch: { "nestedField": { $gte: value } } } } ); Sample Data db.queryToEmbeddedDocument.insertOne({ "UserName": "Larry", "PostDetails": [ { "UserMessage": "Hello", "UserLikes": 8 }, ...

Read More

Search a string with special characters in a MongoDB document?

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

To search a string with special characters in MongoDB document, you can use backslash (\) to escape the special character in a regular expression. Here, we have special character $ in our string. Syntax db.collection.find({ "fieldName": /.*\specialCharacter.*/i }); Sample Data Let us first create a collection with documents ? db.searchDocumentWithSpecialCharactersDemo.insertMany([ { "UserId": "Smith$John123", "UserFirstName": "John", "UserLastName": "Smith" ...

Read More

Can we use NOT and AND together in MongoDB?

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

Yes, we can use NOT and AND together in MongoDB. MongoDB doesn't have direct NOT and AND operators, but we achieve this using $or and $ne operators following De Morgan's law: NOT (X AND Y) = NOT X OR NOT Y. Syntax db.collection.find({ "$or": [ {"field1": {"$ne": "value1"}}, {"field2": {"$ne": "value2"}} ] }); Sample Data db.NotAndDemo.insertMany([ {"StudentName": "John", "StudentCountryName": "US"}, {"StudentName": ...

Read More

How to remove document on the basis of _id in MongoDB?

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

To remove a document based on its _id in MongoDB, use the remove() method with the ObjectId as the matching criteria. Each document has a unique _id field that serves as the primary key. Syntax db.collectionName.remove({"_id": ObjectId("yourObjectId")}); Sample Data Let us first create a collection with sample documents ? db.removeDocumentOnBasisOfId.insertMany([ {"UserName": "Larry", "UserAge": 23, "UserCountryName": "US"}, {"UserName": "Sam", "UserAge": 21, "UserCountryName": "UK"}, {"UserName": "Chris", "UserAge": 24, "UserCountryName": "US"}, {"UserName": "Robert", "UserAge": 26, "UserCountryName": "UK"}, ...

Read More
Showing 1–10 of 61,284 articles
« Prev 1 2 3 4 5 6129 Next »
Advertisements