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
Database Articles
Page 14 of 547
MongoDB query to match documents whose _id is in an array as part of a subdocument?
To match documents whose _id is in an array stored as part of a subdocument, use the $in operator combined with distinct() to extract array values from the subdocument field. Syntax db.targetCollection.find({ "_id": { "$in": db.sourceCollection.distinct("arrayField.subField", {filter}) } }); Sample Data Create a collection with an array of subdocuments containing IDs ? db.demo568.insertOne({ _id: 101, details: [ { id: 101 }, ...
Read MoreMongoDB collection query to exclude some fields in find()?
To exclude specific fields from MongoDB query results, set the unwanted fields to 0 in the projection parameter of the find() method. This allows you to retrieve documents while hiding sensitive or unnecessary data. Syntax db.collectionName.find( { query }, { fieldToExclude1: 0, fieldToExclude2: 0 } ); Sample Data db.demo567.insertMany([ { "Name": "Chris", "Age": 21 }, { "Name": "David", "Age": 23 }, { "Name": "Bob", "Age": 22 }, { "Name": ...
Read MoreGrouping the array items in MongoDB and get the count the products with similar price?
To group array items in MongoDB and count products with similar prices, use the $unwind operator to flatten the array, then $group by price and $sum to count occurrences. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { "_id": "$arrayField.fieldToGroupBy", "count": { $sum: 1 } }}, { $sort: { "_id": 1 } } ]) Sample Data db.products.insertOne({ ...
Read MoreMongoDB - how can I access fields in a document?
To access fields in a MongoDB document, use the find() method with optional field projection to specify which fields to return. You can access all fields or select specific ones using projection syntax. Syntax // Access all fields db.collection.find(query); // Access specific fields using projection db.collection.find(query, {field1: 1, field2: 1, _id: 0}); Sample Data db.demo565.insertMany([ { id: 101, Name: "David", CountryName: "US" ...
Read MoreMake MongoDB replace single array value with string?
To replace a single array value with a string 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 it. Syntax db.collection.updateMany( { "arrayField": "valueToMatch" }, { "$set": { "arrayField.$": "newValue" } } ); Sample Data db.demo564.insertOne({ "StudentName": ["Chris", "David", "Mike", "Sam"] }); { "acknowledged": true, "insertedId": ObjectId("5e90880a39cfeaaf0b97b576") } ...
Read MoreHow to get items with a specific value from documents using MongoDB shell?
To get documents with a specific field value in MongoDB, use the find() method with a query filter. This allows you to retrieve only the documents that match your specified criteria. Syntax db.collection.find( { "field": "value" }, { "field1": 1, "field2": 1 } ) The first parameter is the query filter, and the second optional parameter is the projection to specify which fields to include or exclude. Sample Data db.demo563.insertMany([ { "Name": "Chris", "Age": 21, "isMarried": true }, ...
Read MoreHow to search for a record (field) and then delete it in MongoDB?
To search for a field and then delete it in MongoDB, use $exists to find documents containing the field, then $unset to remove it. The $unset operator deletes a particular field from documents. Syntax db.collection.update( { "fieldName": { $exists: true } }, { $unset: { "fieldName": "" } }, { multi: true } ); Sample Data Let us create a collection with documents ? db.demo562.insertMany([ { "Name": "Chris", "Age": 21 }, { ...
Read MoreRandomizing unique data with MongoDB and placing values for emailid with wordnJohn in the beginning
To randomize unique data with MongoDB and generate email addresses starting with "John", use Math.random() within JavaScript functions to create unique values for each document. Syntax db.collection.find().forEach(function(doc){ db.collection.update( {_id : doc._id}, {$set: {EmailId: 'John' + Math.random() * multiplier + '@domain.com'}} ) }) Sample Data First, create a collection with null EmailId fields ? db.demo561.insertMany([ {EmailId: null}, {EmailId: null}, ...
Read MoreFetch data between two dates and with a specific value in MongoDB. Group and get the sum with count?
To fetch data between two dates and with a specific value in MongoDB, use $match to filter documents and $group to aggregate results. The $gte and $lte operators handle date range filtering. Syntax db.collection.aggregate([ { $match: { "fieldName": "specificValue", "dateField": { "$gte": ISODate("startDate"), ...
Read MoreGROUP BY array of document to get the count of repeated Age values
To GROUP BY array of document to get count of repeated Age values, use the aggregation pipeline with $unwind to flatten the array and $group to count occurrences by Age. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$arrayField.groupingField", count: { $sum: 1 } } }, { $sort: { _id: 1 } } ]); Sample Data db.demo559.insertOne({ details: [ { Name: "Chris", Age: 21 }, ...
Read More