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
Big Data Analytics Articles
Page 63 of 135
Get 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 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 More