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
MongoDB Articles
Page 78 of 111
How to access subdocument value when the key is a number in MongoDB?
To access subdocument values when the key is a number in MongoDB, use bracket notation with quotes around the numeric key. This is necessary because numeric keys are stored as strings in MongoDB subdocuments. Syntax db.collection.findOne().fieldName["numericKey"] db.collection.find({"fieldName.numericKey.subfield": value}) Sample Data db.accessSubDocumentDemo.insertOne({ "Details": { "1": { "StudentLowerScore": "33", "StudentHighScore": "55" ...
Read MoreHow to pull an array element (which is a document) in MongoDB?
To pull (remove) an array element that is a document in MongoDB, use the $pull operator with specific field conditions to match and remove the target document from the array. Syntax db.collection.update( { query }, { $pull: { "arrayField": { "field": "value" } } } ); Sample Data db.pullAnArrayElementDemo.insertOne({ "StudentDetails": [ { "StudentFirstName": "Chris", ...
Read MoreConditional $first in MongoDB aggregation ignoring NULL?
To get the first non-NULL value in MongoDB aggregation, use the $match operator to filter out NULL values before applying $first in the $group stage. Syntax db.collection.aggregate([ { $match: { "fieldName": { $ne: null } } }, { $group: { "_id": "$groupField", "result": { $first: "$fieldName" } }} ]); Sample Data db.conditionalFirstDemo.insertMany([ {_id:100, "StudentName":"Chris", "StudentSubject":null}, ...
Read MoreHow to create a collection correctly in MongoDB to avoid "ReferenceError: Not defined" error?
To create a collection correctly in MongoDB and avoid "ReferenceError: Not defined" errors, you must use the proper MongoDB database object syntax with createCollection() method. Syntax db.createCollection("collectionName"); Example: Creating a Collection Let us create a collection called "employeeInformation" in the "sample" database ? use sample; db.createCollection("employeeInformation"); switched to db sample { "ok" : 1 } Verify Collection Creation Display all collections in the current database to confirm the collection was created ? db.getCollectionNames(); [ "arraySizeErrorDemo", "atleastOneMatchDemo", ...
Read MoreMongoDB 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 More