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
MongoDB Articles
Page 19 of 111
MongoDB query to find on field combination of FirstName and LastName?
To query MongoDB for a field combination of FirstName and LastName, use $concat to combine the fields into a single string and $eq to check equality against your target value. Syntax db.collection.aggregate([ { "$redact": { "$cond": [ { "$eq": [ ...
Read MoreMongoDB query to group duplicate documents
To group duplicate documents in MongoDB, use the $group stage in the aggregation pipeline. This operation groups documents by a specified field and eliminates duplicates by creating a single document per unique value. Syntax db.collection.aggregate([ { $group: { _id: "$fieldName" } } ]); Sample Data db.demo501.insertMany([ { "Name": "Chris" }, { "Name": "Bob" }, { "Name": "Chris" }, { "Name": "John" }, { "Name": "Chris" }, ...
Read MoreMongoDB query to select 10 most recent documents without changing order?
To select the 10 most recent documents in MongoDB without changing their order, use the skip() method with count() - 10. This skips the oldest documents and returns the last 10 in their original insertion order. Syntax db.collection.find().skip(db.collection.count() - 10); Create Sample Data db.demo500.insertMany([ {value: 10}, {value: 1200}, {value: 19}, {value: 28}, {value: 50}, {value: 70}, {value: 100}, {value: 10}, ...
Read MoreQuery array of subdocuments in MongoDB
To query an array of subdocuments in MongoDB, you can use various operators like $unwind in aggregation pipelines, $elemMatch for matching specific array elements, or simple dot notation for basic queries. Syntax // Using $unwind in aggregation db.collection.aggregate([ { $unwind: "$arrayField" }, { $match: { "arrayField.field": "value" } } ]); // Using $elemMatch db.collection.find({ "arrayField": { $elemMatch: { "field": "value" } } }); Sample Data db.demo499.insertOne({ "details": [ ...
Read MoreRetrieving group by result with arrays in MongoDB?
To retrieve group by result with arrays in MongoDB, use aggregate() with $unwind and $group stages. The $addToSet operator adds values to an array unless the value is already present, preventing duplicates. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$fieldToGroupBy", resultArray: { $addToSet: "$fieldToCollect" } } ...
Read MoreChange date format in MongoDB
To change date format in MongoDB, you can manipulate date strings using JavaScript variables and string methods within the MongoDB shell, or use MongoDB's date aggregation operators for more complex transformations. Syntax // JavaScript string manipulation var inputDate = "01-10-2019"; var formatDate = inputDate.split(/-|\//); var outputString = formatDate[2] + '-' + formatDate[0] + '-' + formatDate[1]; // MongoDB aggregation for date formatting db.collection.aggregate([ { $project: { formattedDate: { $dateToString: { format: "%Y-%m-%d", date: "$dateField" } } }} ]); ...
Read MoreHow to find if element exists in document - MongoDB?
To find if an element exists in a MongoDB document, use the $exists operator. This operator checks whether a specified field is present in the document, regardless of its value. Syntax db.collection.find({"fieldName": {$exists: true}}); db.collection.find({"fieldName": {$exists: false}}); Sample Data db.demo497.insertMany([ {"details": [{"Name": "Chris"}, {"Name": "Bob"}]}, {"details": [{"Name": "Carol"}]}, {"details": [{}]} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5e84b3cfb0f3fa88e22790d1"), ...
Read MoreDisplay the undefined and exact MongoDB document records
To display undefined and exact MongoDB document records, use the forEach() method with printjson(). The forEach() callback accepts two parameters: the document and the index, allowing you to display either the actual documents or undefined values. Syntax db.collection.find({}).forEach((document, index) => { printjson(document); // Display documents printjson(index); // Display undefined }); Sample Data db.demo496.insertMany([ { "Name": "David", "CountryName": "US" }, { "Name": "John", "CountryName": "AUS" }, { "Name": "Robert", ...
Read MoreUpdate only a single MongoDB document without deleting any date
To update only a single document in MongoDB, use the updateOne() method. This method updates the first document that matches the specified filter criteria and preserves all existing data except for the fields being updated. Syntax db.collection.updateOne( { "field": "matchValue" }, { $set: { "fieldToUpdate": "newValue" } } ); Create Sample Data Let us create a collection with documents − db.demo495.insertMany([ {"FirstName":"Chris", "Age":19}, {"FirstName":"David", "Age":21}, {"FirstName":"Bob", "Age":26}, {"FirstName":"John", ...
Read MoreUpdate elements inside an array in MongoDB?
To update elements inside an array in MongoDB, use the $set operator combined with the $ positional operator. The positional operator identifies the array element that matches the query condition and allows you to update specific fields within that element. Syntax db.collection.update( { "arrayField.field": "matchValue" }, { $set: { "arrayField.$.fieldToUpdate": "newValue" } } ); Create Sample Data db.demo494.insertOne({ "CollegeDetails": [ { ...
Read More