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 67 of 547
How to Install MongoDB on Ubuntu 16.04
MongoDB is a cross-platform, document oriented database that provides high performance, high availability, and easy scalability. MongoDB works on concept of collection and document. This article explains how to install MongoDB on Ubuntu 16.04 and start the MongoDB service on boot. Adding the MongoDB Repository MongoDB is generally included in Ubuntu package repositories. However, the official MongoDB repository provides the most up-to-date version in a supported manner. First, import the key for the official MongoDB repository using the following command: $ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927 The sample output should be ...
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