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 41 of 111
Check for existing documents/embedded documents in MongoDB
To check for existing documents or embedded documents in MongoDB, use the $exists operator. This operator matches documents where a specified field exists, regardless of its value. Syntax db.collection.find({ "fieldName": { $exists: true } }) db.collection.find({ "embeddedField.subField": { $exists: true } }) Sample Data Let us create a collection with documents containing embedded arrays ? db.demo322.insertMany([ { "id": 1001, "details": [ ...
Read MoreHow to store array values in MongoDB?
MongoDB supports array data types natively, allowing you to store multiple values within a single document field. Arrays can contain simple values, objects, or even nested arrays. Syntax db.collection.insertOne({ "fieldName": [value1, value2, value3] }); // For arrays of objects db.collection.insertOne({ "fieldName": [ {"key1": "value1"}, {"key2": "value2"} ] }); Sample Data Let us create a collection with documents containing array values ? db.demo321.insertMany([ ...
Read MoreMongoDB query for capped sub-collection in an array
In MongoDB, you cannot create capped sub-collections within arrays. However, you can limit the number of array elements returned using the $slice operator in aggregation pipelines to achieve similar functionality. Syntax db.collection.aggregate([ { $project: { fieldName: { $slice: [ "$arrayField", numberOfElements ] } } } ]); Sample Data db.demo319.insertMany([ {"Scores": [100, 345, 980, 890]}, {"Scores": [903, 10004, 84575, 844]} ]); { "acknowledged": true, "insertedIds": [ ...
Read MoreMongoDB query to pull a specific value from a document
To pull a specific value from a document in MongoDB, use the $pull operator with the update() method. The $pull operator removes all instances of a specified value from an existing array. Syntax db.collection.update( { query }, { $pull: { "arrayField": "valueToRemove" } } ) Sample Data Let us create a collection with documents ? db.demo318.insertMany([ { Subject: ["MySQL", "MongoDB", "Java"] }, { Subject: ["Spring", "Hibernate"] } ]) { ...
Read MoreHow to get values greater than a specific value from an embedded list in MongoDB?
To get values greater than a specific value from an embedded list in MongoDB, use the $gt operator with dot notation to target fields within the embedded array. This will return entire documents that contain at least one array element matching the condition. Syntax db.collection.find({ "arrayField.field": { $gt: value } }) Sample Data Let us create a collection with documents containing embedded arrays ? db.demo317.insertMany([ { "id": 101, "details": [ ...
Read MoreHow to restrict inserting an item with the same name in MongoDB?
To restrict inserting items with the same name in MongoDB, create a unique index on the field using createIndex() with unique: true. This prevents duplicate values and throws an error when attempting to insert duplicates. Syntax db.collection.createIndex({"fieldName": 1}, {unique: true}); Example Create a unique index on the SubjectName field to prevent duplicate subject names ? db.demo316.createIndex({"SubjectName": 1}, {unique: true}); { "createdCollectionAutomatically": true, "numIndexesBefore": 1, "numIndexesAfter": 2, "ok": 1 } Insert Unique ...
Read MoreUpdating nested document in MongoDB
To update nested documents in MongoDB, use the $set operator with dot notation to specify the exact path to the field you want to modify. This allows you to update deeply nested fields without affecting other parts of the document. Syntax db.collection.update( { "matchField": "value" }, { $set: { "path.to.nested.field": "newValue" } } ); Sample Data Let us create a collection with a nested document ? db.demo315.insertOne({ _id: 101, details: [ ...
Read MoreMongoDB query to set a sub item in an array?
To set a sub item in an array in MongoDB, use the $ positional operator which identifies the first array element that matches the query condition and allows you to update specific fields within that element. Syntax db.collection.update( {"arrayField.subField": "matchValue"}, {$set: {"arrayField.$.subField": "newValue"}} ); Sample Data Let us create a collection with product documents ? db.demo22.insertOne({ ProductId: 101, ProductDetails: [ { ...
Read MoreHow can I update child objects in MongoDB database?
To update child objects in MongoDB, use the $set operator with dot notation to target specific fields within nested documents. This allows you to modify individual properties of embedded objects without affecting other fields. Syntax db.collection.update( { "matchField": "value" }, { $set: { "parentObject.childField": "newValue" } } ); Sample Data Let us first create a collection with documents ? db.demo21.insertOne({ "StudentId": "STU-101", "StudentDetails": { "StudentName": "Chris", ...
Read MoreShutdown MongoDB with auth enabled?
To shutdown MongoDB with authentication enabled, you need to be authenticated as an administrative user and use the shutdownServer() method on the admin database. Syntax use admin db.auth("username", "password") db.shutdownServer() Method 1: Shutdown with Authentication First, connect to the admin database and authenticate with administrative credentials ? use admin switched to db admin Authenticate as an admin user with shutdown privileges ? db.auth("admin", "password") 1 Now shutdown the MongoDB server ? db.shutdownServer() server should be ...
Read More