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 89 of 547
Unset an attribute from a single array element in MongoDB?
To unset an attribute from a single array element in MongoDB, use the $unset operator combined with the $ positional operator to target the specific array element that matches your query condition. Syntax db.collection.update( {"arrayField.attribute": "matchValue"}, {$unset: {"arrayField.$.attribute": 1}} ) Create Sample Data db.unsetAnAttributeDemo.insertOne({ _id: 1, "StudentDetails": [ { "StudentFirstName": "Ramit", ...
Read MoreSearch by property name for any document with that property in MongoDB?
To search for documents that contain a specific property in MongoDB, use the $ne operator with a null value. This will return all documents where the property exists and is not null. Syntax db.collection.find({ propertyName: { $ne: null } }) Sample Data db.searchByPropertyName.insertMany([ { "FirstName": "Larry", "Age": 23 }, { "FirstName": null, "Age": 21 }, { "FirstName": "John", "Age": 22 }, { "FirstName": null, "Age": 25 }, { "FirstName": "David", "Age": 20 } ...
Read MoreMongoDB query to find the highest numeric value of a column?
To find the highest numeric value of a column in MongoDB, use $type operator with $not to filter only numeric values, then apply sort() and limit() to get the maximum value. Syntax db.collection.find({ "field": { $not: { $type: "string" } } }).sort({ "field": -1 }).limit(1); Sample Data db.highestNumericValueOfAColumnDemo.insertMany([ { "StudentName": "John", "StudentMathMarks": 69 }, { ...
Read MoreDelete a field and value in MongoDB?
To delete a field and its value from MongoDB documents, use the $unset operator. This operator removes the specified field from all matched documents in the collection. Syntax db.collection.update( { field: { $exists: true } }, { $unset: { fieldName: 1 } }, { multi: true } ) Sample Data Let us first create a collection with sample documents − db.deleteFieldDemo.insertMany([ { "FirstName": "John", "LastName": "Smith" }, { "FirstName": "David", "LastName": "Miller" ...
Read MoreHow to read a specific key-value pair from a MongoDB collection?
To read a specific key-value pair from a MongoDB collection, use dot notation in the projection parameter of the find() method. This allows you to retrieve only the nested fields you need. Syntax db.collection.find( {}, { "parentField.childField": 1 } ); Sample Data db.readSpecificKeyValueDemo.insertOne({ "_id": 100, "StudentDetails": { "StudentFirstName": "David", "StudentLastName": "Miller", "StudentAge": 23, ...
Read MoreHow to remove duplicate values inside a list in MongoDB?
To remove duplicate values from an array field in MongoDB, use the aggregation framework with the $setUnion operator. This operator creates a set union between the array and an empty array, automatically eliminating duplicates. Syntax db.collection.aggregate([ { $project: { "fieldName": { $setUnion: ["$arrayField", []] } } } ]); Sample Data db.removeDuplicatesDemo.insertOne({ "InstructorName": "Chris", ...
Read MoreFetch records in MongoDB on querying its subset
To fetch records in MongoDB that contain a subset of values within an array field, use the $all operator. This operator matches documents where the array field contains all the specified elements, regardless of their order or position. Syntax db.collection.find({ "arrayField": { $all: ["value1", "value2", "value3"] } }); Sample Data db.subsetOfAnArrayDemo.insertOne({ "StudentProgrammingSkills": [ "Java", "MongoDB", "MySQL", "C++", "Data Structure", "Algorithm", "Python", "Oracle", "SQL Server" ...
Read MoreExtract subarray value in MongoDB?
To extract a subarray value in MongoDB, you can use the $elemMatch projection operator to return only the first array element that matches the specified condition. Syntax db.collection.find( { query }, { arrayField: { $elemMatch: { field: "value" } } } ); Sample Data Let us first create a collection with documents ? db.extractSubArrayDemo.insertOne({ _id: 101, "clientName": "Larry", "ClientDetails": [ { ...
Read MoreMongoDB query to get specific month|year (not date)?
To get specific month or year values from MongoDB date fields, use the aggregation framework with $month and $year operators. These operators extract the numeric month (1-12) and year values from date fields. Syntax db.collection.aggregate([ { $project: { field: 1, dateField: { $month: "$dateField" } } }, { $match: { dateField: monthNumber } } ]); Sample Data db.specificMonthDemo.insertMany([ { "StudentName": "Larry", "StudentDateOfBirth": new ISODate("1995-01-12") }, { "StudentName": "Chris", "StudentDateOfBirth": new ISODate("1999-12-31") }, { ...
Read MoreHow do I add a value to the top of an array in MongoDB?
To add a value to the top of an array in MongoDB, you can use the $push operator with $each and $position: 0 to insert elements at the beginning of an array field in a document. Syntax db.collection.updateOne( { query }, { $push: { arrayField: { $each: ["newValue"], ...
Read More