Database Articles

Page 67 of 547

How to Install MongoDB on Ubuntu 16.04

Sharon Christine
Sharon Christine
Updated on 15-Mar-2026 641 Views

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 More

Count number of elements in an array with MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 1K+ Views

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 More

Get distinct first word from a string with MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 267 Views

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 More

How to query all items in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 370 Views

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 More

Display a value with $addToSet in MongoDB with no duplicate elements?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 247 Views

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 More

Extract particular element in MongoDB within a Nested Array?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 689 Views

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 More

How to dynamically build MongoDB query?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 2K+ Views

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 More

Retrieving an embedded object as a document via the aggregation framework in MongoDB?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 359 Views

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 More

Query array of nested string with MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 266 Views

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 More

Replace an array field value with MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 363 Views

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
Showing 661–670 of 5,468 articles
« Prev 1 65 66 67 68 69 547 Next »
Advertisements