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
Database Articles
Page 81 of 547
MongoDB query for fields in embedded document?
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 MoreProject specific array field in a MongoDB collection?
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 MoreHow do you update a MongoDB document while replacing the entire document?
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 MoreHow to keep two "columns" in MongoDB unique?
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 MoreHow to unset a variable in MongoDB shell?
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 MoreMongoDB Query to combine AND & OR?
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 MoreMongoDB query to count the size of an array distinctly?
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 MoreHow to rename a username in MongoDB?
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 MorePerforming distinct on multiple fields in MongoDB?
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 MoreHow do I search according to fields in inner classes using MongoDB db.coll.find()?
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