Database Articles

Page 89 of 547

Unset an attribute from a single array element in MongoDB?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 236 Views

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 More

Search by property name for any document with that property in MongoDB?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 279 Views

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 More

MongoDB query to find the highest numeric value of a column?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 248 Views

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 More

Delete a field and value in MongoDB?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 491 Views

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 More

How to read a specific key-value pair from a MongoDB collection?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 985 Views

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 More

How to remove duplicate values inside a list in MongoDB?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 1K+ Views

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 More

Fetch records in MongoDB on querying its subset

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 181 Views

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 More

Extract subarray value in MongoDB?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 467 Views

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 More

MongoDB query to get specific month|year (not date)?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 766 Views

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 More

How do I add a value to the top of an array in MongoDB?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 266 Views

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
Showing 881–890 of 5,468 articles
« Prev 1 87 88 89 90 91 547 Next »
Advertisements