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 107 of 135
Find MongoDB records where array field is not empty?
To find MongoDB records where an array field is not empty, use the $ne (not equal) operator with an empty array []. You can also combine it with $exists to ensure the field exists and is not empty. Syntax db.collection.find({ "arrayField": { $exists: true, $ne: [] } }); Sample Data db.arrayFieldIsNotEmptyDemo.insertMany([ {"StudentName": "Larry", "StudentTechnicalSubject": ["Java", "C"]}, {"StudentName": "Mike", "StudentTechnicalSubject": []}, {"StudentName": "Sam", "StudentTechnicalSubject": ["MongoDB"]}, {"StudentName": "Carol", "StudentTechnicalSubject": []}, {"StudentName": ...
Read MoreCheck that Field Exists with MongoDB?
In MongoDB, you can check if a field exists in documents using the $exists operator. To ensure the field is not null, combine it with the $ne operator. Syntax db.collection.find({ "fieldName": { $exists: true, $ne: null } }) Sample Data db.checkFieldExistDemo.insertMany([ { "EmployeeId": 1, "EmployeeName": "John", "isMarried": true, "EmployeeSalary": 4648585 }, ...
Read MoreGet MongoDB documents with max attribute per group in a collection?
To get documents with the maximum attribute per group in a MongoDB collection, use the $sort operator combined with the $group stage in an aggregation pipeline. This approach sorts documents by the grouping field and the target attribute, then uses $first to select the document with the highest value per group. Sample Data db.maxAttributePerGroup.insertMany([ { "StudentFirstName": "John", "StudentLastName": "Smith", "StudentAge": 29, ...
Read MoreHow to replace substring in MongoDB document?
In MongoDB, you can replace substrings in documents using JavaScript's replace() function within a forEach() loop. This approach finds documents, modifies the substring, and updates them back to the collection. Sample Data db.replaceSubstringDemo.insertOne({ "WebsiteURL": "www.gogle.com" }); { "acknowledged": true, "insertedId": ObjectId("5c76eaf21e9c5dd6f1f78276") } Let's verify the document was inserted ? db.replaceSubstringDemo.find().pretty(); { "_id": ObjectId("5c76eaf21e9c5dd6f1f78276"), "WebsiteURL": "www.gogle.com" } Replace Substring Using forEach() To replace "gogle" with ...
Read MoreGet a count of total documents with MongoDB while using limit?
To get a count of total documents while also limiting the results, use the $facet operator in MongoDB. This allows you to run multiple aggregation pipelines on the same data simultaneously − one for the limited data and another for the total count. Syntax db.collection.aggregate([ { $facet: { data: [{ $limit: number }], totalCount: [{ $count: "total" }] ...
Read MoreMongoDB Regex Search on Integer Value?
To perform regex search on integer values in MongoDB, use the $where operator with JavaScript regex testing. This allows pattern matching on numeric fields by converting them to strings during evaluation. Syntax db.collection.find({ $where: "/^yourPattern.*/.test(this.fieldName)" }); Sample Data db.regExpOnIntegerDemo.insertMany([ { "StudentId": 2341234 }, { "StudentId": 123234 }, { "StudentId": 9871234 }, { "StudentId": 2345612 }, { "StudentId": 1239812345 } ]); { ...
Read MoreHow to sort inner array in MongoDB?
To sort inner arrays in MongoDB, use the aggregation framework with $unwind, $sort, $group, and $project stages. This approach deconstructs the array, sorts the elements, and reconstructs the sorted array. Sample Data db.sortInnerArrayDemo.insertOne({ "EmployeeDetails": { "EmployeeAddress": { "EmployeeCountry": [ { ...
Read MoreQuerying internal array size in MongoDB?
To query internal array size in MongoDB, use the $size operator. This operator returns the number of elements in an array field and can be used within aggregation pipelines to count array elements for specific documents. Syntax db.collection.aggregate([ { $group: { _id: yourObjectIdValue, fieldName: { $first: { $size: "$arrayFieldName" } } } ...
Read MoreAdd new field to every document in a MongoDB collection?
To add new field to every document in a MongoDB collection, you can use $set operator. The syntax is as follows ? Syntax db.yourCollectionName.update({}, { $set: {"yourFieldName": yourValue} }, false, true); Sample Data To understand the above syntax, let us create a collection with some documents. The query to create a collection with documents is as follows ? db.addNewFieldToEveryDocument.insertMany([ {"StudentName": "John", "StudentAddress": "US"}, {"StudentName": "David", "StudentAddress": "UK"}, {"StudentName": "Carol", "StudentAddress": "UK"}, {"StudentName": "Bob", "StudentAddress": "US"} ]); ...
Read MoreHow to remove a field completely from a MongoDB document?
You can use the $unset operator to remove a field completely from a MongoDB document. The syntax is as follows ? Syntax db.yourCollectionName.update({}, {$unset: {yourFieldName:1}}, false, true); Sample Data To understand the above syntax, let us create a collection with some documents. The query to create a collection with documents is as follows ? db.removeFieldCompletlyDemo.insertOne({"StudentName":"Larry", "StudentFavouriteSubject": ["Java", "C", "C++", "Python"]}); db.removeFieldCompletlyDemo.insertOne({"StudentName":"Mike", "StudentFavouriteSubject": ["Javascript", "HTML5", "CSS3"]}); db.removeFieldCompletlyDemo.insertOne({"StudentName":"Sam", "StudentFavouriteSubject": ["MongoDB", "MySQL", "SQL Server"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ef55a6fd07954a48906a3") } ...
Read More