Database Articles

Page 81 of 547

MongoDB query for fields in embedded document?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 290 Views

To query fields in embedded documents in MongoDB, use dot notation to navigate through the nested structure. For arrays of embedded documents, MongoDB searches through all array elements and returns the entire document when a match is found. Syntax db.collection.find({"arrayName.fieldName": value}); Sample Data Let us first create a collection with embedded documents: db.embeddedDocumentDemo.insertOne({ "CustomerDetails": [ {"CustomerName": "Chris", "CustomerPurchasePrice": 3000}, {"CustomerName": "Robert", "CustomerPurchasePrice": 4500}, {"CustomerName": ...

Read More

Project specific array field in a MongoDB collection?

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

To project specific fields from an array in MongoDB, use dot notation in the projection document to select only the desired fields from nested array elements. This allows you to return specific fields from array objects while excluding unwanted data. Syntax db.collection.find( {}, { "fieldName": 1, "arrayField.nestedField": 1 } ); Create Sample Data db.projectionAnElementDemo.insertOne( { ...

Read More

How do you update a MongoDB document while replacing the entire document?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 194 Views

To update a MongoDB document while replacing the entire document, use the update() or replaceOne() method without update operators like $set. This replaces all fields except the _id field. Syntax db.collection.update( { filter }, { newDocument } ); // Or use replaceOne() (recommended) db.collection.replaceOne( { filter }, { newDocument } ); Sample Data Let us first create a collection with a document: db.replacingEntireDocumentDemo.insertOne({ "StudentFirstName": "John", "StudentLastName": "Smith", ...

Read More

How to keep two "columns" in MongoDB unique?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 1K+ Views

To keep two fields unique together in MongoDB, create a compound unique index on both fields. This ensures that the combination of both field values must be unique across the collection. Syntax db.collection.createIndex( {"field1": 1, "field2": 1}, {unique: true} ); Example: Create Compound Unique Index Create a unique index on StudentFirstName and StudentLastName fields ? db.keepTwoColumnsUniqueDemo.createIndex( {"StudentFirstName": 1, "StudentLastName": 1}, {unique: true} ); { "createdCollectionAutomatically": true, ...

Read More

How to unset a variable in MongoDB shell?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 689 Views

Use the delete operator to unset (remove) a variable from the MongoDB shell environment. This permanently removes the variable from memory. Syntax delete variableName; Example 1: Check Undefined Variable First, let's check if a variable exists before defining it ? customerDetail; 2019-05-08T22:29:17.361+0530 E QUERY [js] ReferenceError: customerDetail is not defined : @(shell):1:1 Example 2: Define and Use Variable Now let's create the variable with a value ? customerDetail = {"CustomerFirstName": "Chris"}; { "CustomerFirstName" : "Chris" } Display the variable ...

Read More

MongoDB Query to combine AND & OR?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 2K+ Views

To combine AND & OR operators in MongoDB, use the $and and $or operators together to create complex query conditions. This allows you to match documents that satisfy multiple logical combinations. Syntax db.collection.find({ "$or": [ { "$and": [{ field1: value1 }, { field2: value2 }] }, { field3: value3 } ] }); Sample Data db.combinedAndOrDemo.insertMany([ { "StudentFirstName": ...

Read More

MongoDB query to count the size of an array distinctly?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 318 Views

To count the size of an array distinctly in MongoDB, use the distinct() method to get unique elements and then apply .length to count them. This eliminates duplicate values and returns only the count of unique elements. Syntax db.collection.distinct('fieldName').length; Sample Data Let us create a collection with duplicate student names ? db.countOrSizeDemo.insertMany([ {"StudentFirstName": "John"}, {"StudentFirstName": "David"}, {"StudentFirstName": "David"}, {"StudentFirstName": "Carol"}, {"StudentFirstName": "Sam"}, {"StudentFirstName": "John"} ]); ...

Read More

How to rename a username in MongoDB?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 2K+ Views

To rename a username in MongoDB, you need to update the user document in the system.users collection using the $set operator. This operation modifies the user field while preserving all other user properties like roles and authentication mechanisms. Syntax db.system.users.update( {"user": "oldUserName"}, {$set: {"user": "newUserName"}} ); Display Current Users First, let's view all existing users in the database ? use admin; db.getUsers(); [ { "_id": "admin.Chris", ...

Read More

Performing distinct on multiple fields in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 3K+ Views

To perform distinct on multiple fields in MongoDB, use the $group operator with the aggregation framework. The $addToSet operator collects unique values for each field across all documents. Syntax db.collection.aggregate([ { $group: { _id: null, field1: { $addToSet: "$field1" }, field2: { $addToSet: "$field2" }, ...

Read More

How do I search according to fields in inner classes using MongoDB db.coll.find()?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 219 Views

Use dot notation (.) to search in inner classes (nested objects) using MongoDB. This allows you to query specific fields within embedded documents efficiently. Syntax db.collection.find({"outerField.innerField": "value"}) Sample Data db.searchInInnerDemo.insertMany([ { "StudentFirstName": "Robert", "StudentTechnicalDetails": { "StudentBackEndTechnology": "MongoDB", "StudentLanguage": "Java" } ...

Read More
Showing 801–810 of 5,468 articles
« Prev 1 79 80 81 82 83 547 Next »
Advertisements