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 20 of 111
Make nested queries in MongoDB 4 to fetch a specific document
To make nested queries in MongoDB 4 to fetch a specific document, use dot notation to traverse through nested objects and arrays. This allows you to query deeply nested fields within documents. Syntax db.collection.find({ "parentField.childField.nestedField": "value" }); Sample Data Let us first create a collection with nested documents ? db.demo492.insertMany([ { "ProductDetails": { "StockDetails": [ ...
Read MoreHow to match date with MongoDB $match?
To match date in MongoDB, use $match operator within the aggregation pipeline. The $match stage filters documents based on date criteria using comparison operators like $gte, $lte, or $eq. Syntax db.collection.aggregate([ { $match: { "dateField": { "$operator": ISODate("YYYY-MM-DDTHH:mm:ss.sssZ") } ...
Read MoreMongoDB query to fetch random value using Map Reduce concept.
To fetch random values using Map Reduce in MongoDB, use the mapReduce() method combined with Math.random() to emit documents based on a probability threshold. Syntax db.collection.mapReduce( function() { if (Math.random() < threshold) emit(this._id, this); }, function(key, values) { return values; }, { out: "outputCollection" } ); Sample Data db.demo651.insertMany([ {Value: 10}, {Value: 20}, {Value: 30}, {Value: 40}, {Value: 50}, ...
Read MoreQuerying only the field name and display only the id in MongoDB?
To query only specific fields in MongoDB, use projection to control which fields appear in the result. Set field values to 1 to include or 0 to exclude them from the output. Syntax db.collection.find(query, projection); // Include specific fields db.collection.find({}, {fieldName: 1}); // Exclude specific fields db.collection.find({}, {fieldName: 0}); Sample Data db.demo650.insertMany([ {_id: 101, details: {Name: "Chris", Age: 21}}, {_id: 102, details: {Name: "Bob", Age: 22}}, {_id: 103, details: {Name: "Sam", Age: 20}}, ...
Read MoreMongoDB aggregation group and remove duplicate array values?
To group documents and remove duplicate values from arrays in MongoDB, use the aggregation framework with $unwind to deconstruct arrays and $addToSet within $group to collect unique values. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$groupByField", arrayField: { $addToSet: "$arrayField" }, otherField: { $first: "$otherField" } }} ]); Sample Data db.demo649.insertMany([ ...
Read MoreMongoDB aggregation to combine or merge fields and then count?
To combine or merge fields and then perform count, use $group along with $sum and $sort. This aggregation approach groups documents by field values and counts occurrences in each group. Syntax db.collection.aggregate([ { $group: { "_id": "$fieldName", "COUNT": { $sum: 1 } } }, { $sort: { "COUNT": -1 } }, { $limit: n } ]); Sample Data db.demo647.insertMany([ {"Subject": "MySQL"}, {"Subject": "MongoDB"}, {"Subject": "MySQL"}, ...
Read MoreMove different elements to another array in MongoDB?
To move different elements to another array in MongoDB, use forEach with filter() methods to separate elements based on criteria and save() to update the document. This approach allows you to move specific elements from one array to another within the same document. Syntax db.collection.find({}).forEach(function(doc) { doc.targetArray = doc.sourceArray.filter(function(element) { return condition; }); doc.sourceArray = doc.sourceArray.filter(function(element) { return !condition; }); ...
Read MoreMongoDB query to store File Name and location?
To store file name and location in MongoDB, create documents with fileName and fileLocation fields. This approach is useful for file management systems, content libraries, or any application that needs to track file metadata. Syntax db.collection.insertOne({ "fileName": "filename.extension", "fileLocation": "/path/to/file/location" }); Sample Data Let us create a collection with file documents using insertMany() ? db.demo645.insertMany([ { "fileName": "MongoDB Program", "fileLocation": "C:/users/workspace/AllMongoDBProgram/MongoDB Program" ...
Read MoreHow to update a Timestamp and set to current date in MongoDB?
To update a timestamp and set it to the current date in MongoDB, use the update() method with $set operator and JavaScript's new Date() to get the current timestamp. Syntax var currentDate = new Date(); db.collection.update( {}, { $set: { "timestampField": currentDate } }, { multi: true } ); Sample Data Let us first create a collection with documents ? db.demo644.insertMany([ { "ShippingDate": new ISODate("2018-04-19") }, { "ShippingDate": new ISODate("2019-01-10") ...
Read MoreUpdating Nested Embedded Documents in MongoDB?
To update nested embedded documents in MongoDB, use the positional operator ($) with update operations like $push, $set, or $addToSet. The positional operator identifies the array element that matches the query condition and allows you to modify nested documents within that element. Syntax db.collection.update( {"arrayField.nestedField": "matchValue"}, {"$push": {"arrayField.$.nestedArray": newDocument}} ); Sample Data Let's create a collection with nested embedded documents ? db.demo643.insertOne({ details: [ { ...
Read More