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 21 of 111
MongoDB query to get specific list of names from documents where the value of a field is an array
To get specific list of names from documents where the value of a field is an array, use the $all operator. The $all operator selects documents where the array field contains all the specified elements. Syntax db.collection.find({ arrayField: { $all: [ "element1", "element2", ... ] } }); Sample Data Let us create a collection with documents containing arrays of names ? db.demo642.insertMany([ { _id: ...
Read MoreGet specific elements from embedded array in MongoDB?
To get specific elements from an embedded array in MongoDB, use the $unwind operator to deconstruct the array, then $match with dot notation to filter elements based on specific criteria. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $match: { "arrayField.fieldName": value } }, { $project: { _id: 0, arrayField: 1 } } ]); Sample Data db.demo641.insertOne({ ProductId: 101, "ProductInformation": [ { ...
Read MoreFiltering MongoDB items by fields and subfields?
To filter MongoDB documents by fields and subfields, use dot notation to access nested properties. This allows you to query specific values within embedded documents or check for field existence. Syntax db.collection.find({"field.subfield": value}); db.collection.find({"field.subfield": null}); db.collection.find({"field.subfield": {$exists: true}}); Sample Data db.demo638.insertMany([ {Name: "Chris"}, {Name: "David", details: {Subject: "MongoDB"}}, {Name: "John", details: {Subject: "JavaScript", Level: "Advanced"}} ]); WriteResult({ "nInserted" : 3 }) Display all documents from the collection ? db.demo638.find().pretty(); { ...
Read MoreMongoDB - interpret information on the query plan for the db.collection.find() method
To interpret information on the query plan for the db.collection.find() method in MongoDB, use the explain() method. This provides detailed insights into query execution including index usage and performance characteristics. Syntax db.collection.find(query).explain() db.collection.find(query).explain("executionStats") Sample Data Create a collection with an index and insert sample documents: db.demo637.createIndex({ClientName: 1}); db.demo637.insertMany([ {ClientName: "John"}, {ClientName: "Bob"}, {ClientName: "Johnson"} ]); { "createdCollectionAutomatically": true, "numIndexesBefore": 1, "numIndexesAfter": 2, ...
Read MoreMongoDB query to display alternative documents with mapReduce() function and emit even field values
To display alternative documents with the mapReduce() function and emit even field values in MongoDB, use a custom counter in the scope parameter to track document order and emit only documents at even positions. Syntax db.collection.mapReduce( function() { counter++; var id = this._id; delete this._id; if (counter % divisor != 0) ...
Read MoreCalculate frequency of duplicate names from NAME field using MongoDB aggregate?
To calculate the frequency of duplicate names in MongoDB, use the $group stage in the aggregation pipeline to group documents by the Name field and count occurrences with $sum. Syntax db.collection.aggregate([ { $group: { "_id": "$fieldName", count: { $sum: 1 } } } ]); Create Sample Data db.demo635.insertMany([ {Name: "Chris"}, {Name: "David"}, {Name: "David"}, {Name: "Chris"}, {Name: "Bob"}, {Name: "Chris"} ]); { ...
Read MoreHow to update document with marks value in MongoDB for student David
To update a specific document's marks value for student David in MongoDB, you can use the forEach() method to traverse the collection and find the student, then update their marks using the $set operator. Syntax db.collection.find({Name: "StudentName"}).forEach(function(doc) { db.collection.update({_id: doc._id}, {$set: {Marks: newValue}}); }); Sample Data Let us create a collection with student documents ? db.demo634.insertMany([ {Name: "Chris", Marks: 76}, {Name: "Bob", Marks: 67}, {Name: "David", Marks: 37} ]); { ...
Read MoreRemove values from a matrix like document in MongoDB
To remove values from a matrix-like document in MongoDB, use the $pull operator with dot notation to target specific elements within nested arrays. This operator removes all instances of a specified value from an array field. Syntax db.collection.update( { query }, { $pull: { "arrayMatrix.index": value } } ); Sample Data Let's create a collection with a matrix-like document ? db.demo632.insertOne({ "arrayMatrix": [ [10, 20], ...
Read MoreReturn specific MongoDB embedded document
To return a specific embedded document in MongoDB, use the $unwind operator twice to flatten nested arrays, followed by $match to filter the desired embedded document based on specific criteria. Syntax db.collection.aggregate([ { "$unwind": "$arrayField1" }, { "$unwind": "$arrayField1.arrayField2" }, { "$match": { "arrayField1.arrayField2.field": "value" } } ]); Sample Data Let us create a collection with nested embedded documents ? db.demo631.insertOne({ id: "101", Info1: [ ...
Read MoreQuery for values (not objects) in list with MongoDB
To query for values in list, use the positional operator ($) in MongoDB. The $ operator projects only the first matching array element that satisfies the query condition. Syntax db.collection.find( { "arrayField": "value" }, { "arrayField.$": 1 } ); Sample Data Let us create a collection with documents — db.demo628.insertMany([ { id: 1, Name: ["Chris", "David", "John"] }, { id: 1, Name: ["Carol", "Sam"] }, { id: 2, Name: ["Mike", "Sam", "John"] ...
Read More