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 find latest entries in array over all MongoDB documents?
To find latest entries in array over all MongoDB documents, use the aggregate() method with $unwind to flatten arrays, $sort to order by the desired field, and $limit to get the top entries. Syntax db.collection.aggregate([ { "$unwind": "$arrayField" }, { "$sort": { "arrayField.sortField": -1 } }, { "$limit": numberOfEntries } ]); Sample Data db.demo179.insertMany([ { "Name": "Chris", "Details": [ ...
Read MoreMongoDB query to fetch date records (ISODate format) in a range
To fetch date records within a specific range from MongoDB documents containing ISODate format dates, use range operators like $gte, $lte, $gt, and $lt in your queries, or aggregation pipeline operators for more complex date filtering. Syntax // Basic date range query db.collection.find({ "dateField": { "$gte": ISODate("start_date"), "$lte": ISODate("end_date") } }); // Aggregation with time-based filtering db.collection.aggregate([ { "$redact": { ...
Read MoreMongoDB query to add a document in an already created collection
To add a document to an existing collection in MongoDB, use the $push operator. This operator appends new documents to an existing array field within a document. Syntax db.collection.update( { "field": "value" }, { $push: { "arrayField": { newDocument } } } ); Sample Data Let's create a collection with sample documents ? db.demo177.insertOne({ "Id": "101", "details": [ { "StudentName": "Chris", ...
Read MoreGet substring in MongoDB aggregate
To extract substring in MongoDB aggregation pipelines, use the $substr operator within the $project stage. This operator extracts a portion of a string starting from a specified position and length. Syntax db.collection.aggregate([ { $project: { fieldName: { $substr: ["$sourceField", startIndex, length] } } } ]) Sample Data db.demo176.insertMany([ {"ProductName": "PRODUCT-1"}, ...
Read MoreHow do I use MongoDB to count only collections that match two fields?
To count documents in MongoDB that match multiple field conditions, use the count() method with a query document containing all required field−value pairs. MongoDB will return the count of documents where all specified conditions are satisfied. Syntax db.collection.count({ "field1": "value1", "field2": "value2" }); Sample Data db.demo175.insertMany([ {"EmployeeName": "Bob", "isMarried": "YES"}, {"EmployeeName": "David", "isMarried": "NO"}, {"EmployeeName": "Mike", "isMarried": "YES"}, {"EmployeeName": "Sam", "isMarried": "NO"} ]); { ...
Read MoreEvaluate one of more values from a MongoDB collection with documents
To evaluate one or more values from a MongoDB collection, use the $or operator with the find() method. The $or operator performs a logical OR operation on an array of expressions and returns documents that match at least one of the conditions. Syntax db.collection.find({ $or: [ { "field1": "value1" }, { "field2": "value2" }, { "fieldN": "valueN" } ] }); Sample Data ...
Read MoreFind a value in lowercase from a MongoDB collection with documents
To find a value in lowercase from a MongoDB collection, use the toLowerCase() method in JavaScript when constructing your query. This allows you to search for documents where a field matches a specific value in lowercase format. Syntax db.collection.find({"fieldName": "VALUE".toLowerCase()}); Sample Data Let us create a collection with documents containing subject names in different cases ? db.demo172.insertMany([ {"SubjectName": "MySQL"}, {"SubjectName": "mongodb"}, {"SubjectName": "MongoDB"} ]); { "acknowledged": true, "insertedIds": [ ...
Read MoreHow to re-map the fields of a MongoDB collection?
To re-map the fields of a MongoDB collection, use $rename operator with update() or updateMany(). This operator allows you to rename field names across documents in your collection. Syntax db.collection.updateMany( {}, { $rename: { "oldFieldName": "newFieldName" } } ); Sample Data Let us create a collection with sample documents ? db.demo171.insertMany([ { "Name": "Chris", "Details": { ...
Read MoreHow to Secure MongoDB on Ubuntu 16.04
In this article, we will learn how to secure MongoDB on Ubuntu 16.04. In previous versions, MongoDB was vulnerable to automated exploits because, by default, there was no authentication required to interact with the database. Any user could create, read, modify and destroy databases and their contents, as the MongoDB daemon listens on all interfaces by default. Enabling Authentication and Adding Admin User This issue has been mitigated in MongoDB versions 3.x and later, however, authentication is still disabled by default. To secure MongoDB, we will create an administrative user and enable authentication. Step 1: Connect to ...
Read MoreHow 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 More