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
Articles on Trending Technologies
Technical articles with clear explanations and examples
How To Install and Configure MongoDB on CentOS 7
In this article, we will learn how to install and configure MongoDB on CentOS 7. MongoDB is an open-source NoSQL database that stores documents in a JSON-like format called BSON. Unlike traditional relational databases, MongoDB doesn't require a predefined schema and offers high availability, performance, and automatic scaling capabilities. Prerequisites CentOS 7 installed on the Linux machine A user with root privileges Adding the MongoDB Repository By default, the MongoDB repository is not available in CentOS 7. We need to add the MongoDB repository to the local machine first. vi /etc/yum.repos.d/mongodb-org.repo ...
Read MoreCount number of elements in an array with MongoDB?
To count the number of elements in an array in MongoDB, use the $size operator within the aggregation framework. The $size operator returns the number of elements in the specified array field. Syntax db.collection.aggregate([ { $project: { fieldName: { $size: "$arrayField" } } } ]); Sample Data Let us create a sample collection with array data ? ...
Read MoreGet distinct first word from a string with MongoDB?
To get distinct first words from a string field in MongoDB, use the distinct() method combined with JavaScript's map() and split() functions to extract and return the first word from each string. Syntax db.collection.distinct("fieldName", query).map(function(str) { return str.split(" ")[0]; }); Sample Data db.distinctFirstWordDemo.insertMany([ { "_id": 100, "StudentName": "John", "StudentFeature": "John is a good player", ...
Read MoreHow to query all items in MongoDB?
To query all items in a MongoDB collection, use the find() method. This method retrieves all documents from a collection when called without parameters, or you can specify query criteria and projection options to filter results. Syntax db.collection.find() db.collection.find(query, projection) Create Sample Data Let us first create a collection with documents − db.queryAllItemsDemo.insertMany([ { "StudentDetails": { "StudentName": "John", ...
Read MoreDisplay a value with $addToSet in MongoDB with no duplicate elements?
The $addToSet operator in MongoDB ensures that duplicate elements are not added to an array field. When used with aggregation pipeline, it creates a set of unique values from the specified field. Syntax db.collection.aggregate([ { $group: { _id: null, fieldName: { $addToSet: "$arrayField" } } } ]); ...
Read MoreExtract particular element in MongoDB within a Nested Array?
To extract particular elements in MongoDB within nested arrays, use the $elemMatch operator to match array elements and projection to return only specific fields from the matched documents. Syntax db.collection.find( { "arrayField": { $elemMatch: { "nestedArrayField": { ...
Read MoreHow to dynamically build MongoDB query?
Dynamic MongoDB queries allow you to build query conditions programmatically based on runtime parameters. This is useful when query criteria depend on user input or application logic. Syntax function dynamicQuery(parameters) { var query = {}; // Build query conditions based on parameters if (condition) { query["field"] = { "$operator": value }; } return query; } Sample Data db.dynamicQueryDemo.insertMany([ { "Name": "John", ...
Read MoreRetrieving an embedded object as a document via the aggregation framework in MongoDB?
To retrieve an embedded object as a document in MongoDB, use the aggregation framework's $replaceRoot stage. This operator promotes the specified embedded document to the top level, effectively replacing the entire document structure. Syntax db.collection.aggregate([ { $replaceRoot: { newRoot: "$embeddedFieldName" } } ]); Create Sample Data db.embeddedObjectDemo.insertMany([ { "UserDetails": { "UserName": "John", "UserAge": ...
Read MoreQuery array of nested string with MongoDB?
To query array of nested string in MongoDB, you can use dot notation to access nested array elements. This allows you to search for specific string values within arrays that are nested inside other objects. Syntax db.collection.find({"parentField.nestedArrayField": "searchValue"}) Sample Data Let us first create a collection with documents containing nested arrays of strings ? db.nestedStringDemo.insertMany([ { "CustomerName": "John", "CustomerOtherDetails": [ ...
Read MoreReplace an array field value with MongoDB?
To replace an array field value in MongoDB, use the $ positional operator with $set. The positional operator identifies the array element that matches the query condition and replaces it with the new value. Syntax db.collection.update( { "arrayField": "valueToReplace" }, { $set: { "arrayField.$": "newValue" } } ); Sample Data db.replaceAnArrayFieldValueDemo.insertOne({ "StudentTechnicalSubjects": ["MySQL", "SQL Server", "PL/SQL"] }); { "acknowledged": true, "insertedId": ObjectId("5cea41e0ef71edecf6a1f68f") } View Current Document ...
Read More