Anvi Jain

Anvi Jain

427 Articles Published

Articles by Anvi Jain

Page 8 of 43

MongoDB query to remove array elements from a document?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 292 Views

Use the $pull operator to remove array elements from a MongoDB document. This operator removes all instances of a value from an existing array field. Syntax db.collection.update( { }, { $pull: { fieldName: value } }, { multi: true } ); Sample Data Let us first create a collection with documents − db.removeArrayElementsDemo.insertMany([ { "AllPlayerName": ["John", "Sam", "Carol", "David"] }, { "AllPlayerName": ["Chris", "Robert", "John", "Mike"] } ]); { ...

Read More

MongoDB query to replace value in an array?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 1K+ Views

To replace a specific value in a MongoDB array, 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.update( { "arrayField": "valueToReplace" }, { $set: { "arrayField.$": "newValue" } } ); Sample Data db.replaceValueInArrayDemo.insertMany([ { "StudentScores": [45, 56, 78] }, { "StudentScores": [33, 90, 67] } ]); { "acknowledged": true, ...

Read More

Check for null in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 642 Views

In MongoDB, you can check for null values using the $type operator with type number 10 or alias "null". This operator specifically matches fields that contain explicit null values, not missing fields or empty strings. Syntax db.collection.find({"fieldName": { $type: 10 }}); // OR db.collection.find({"fieldName": { $type: "null" }}); Sample Data Let's create a collection with different types of values to demonstrate null checking ? db.mongoDbEqualDemo.insertMany([ {"Age": 34}, {"Age": ""}, {"Age": null}, {"Age": 56}, ...

Read More

Use ObjectId under findOne() to fetch a specific record in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 519 Views

To fetch a specific record in MongoDB using ObjectId with the findOne() method, pass the ObjectId as the query criteria. The findOne() method returns the first document that matches the specified ObjectId. Syntax db.collection.findOne({"_id": ObjectId("objectid_value")}); Create Sample Data db.findOneWorkingDemo.insertMany([ {"ClientId": 1, "ClientName": "Larry", "ClientAge": 26}, {"ClientId": 2, "ClientName": "Chris", "ClientAge": 28}, {"ClientId": 3, "ClientName": "Robert", "ClientAge": 34} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

Is there any way to see the MongoDB results in a better format?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 179 Views

Yes, MongoDB provides several methods to display query results in a better, more readable format instead of the default single-line output. The most common approaches are findOne() and find().toArray(). Syntax // Display single document in formatted JSON db.collection.findOne(); // Display all documents in formatted JSON array db.collection.find().toArray(); Sample Data Let us first create a collection with sample documents ? db.betterFormatDemo.insertMany([ {"StudentName": "Adam Smith", "StudentScores": [98, 67, 89]}, {"StudentName": "John Doe", "StudentScores": [67, 89, 56]}, {"StudentName": "Sam Williams", "StudentScores": [45, ...

Read More

How to exclude _id without including other fields using the aggregation framework in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 906 Views

To exclude the _id field without explicitly including other fields in MongoDB's aggregation framework, use the $project stage with _id: 0 and selectively include only the fields you need. Syntax db.collection.aggregate([ { $project: { _id: 0, "field1": 1, "field2": 1 } ...

Read More

MongoDB Query to search for records only in a specific hour?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 642 Views

To search for records only in a specific hour in MongoDB, use the $hour operator within an aggregation pipeline. The $hour operator extracts the hour component (0-23) from a date field. Syntax db.collection.aggregate([ { $project: { fieldName: { $hour: "$dateField" } } }, { $match: { fieldName: { $in: [hour1, hour2] } } } ]); Sample Data db.mongoDbSearchForHoursDemo.insertMany([ { "CustomerName": "Larry", "OrderDatetime": new ISODate("2019-01-31 09:45:50") ...

Read More

Add MD5 hash value to MongoDB collection?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 2K+ Views

To add MD5 hash values to a MongoDB collection, use the hex_md5() function combined with forEach() to iterate through documents and generate hash values for specific fields like passwords. Syntax db.collection.find().forEach(function(doc) { doc.hashField = hex_md5(doc.sourceField); db.collection.save(doc); }); Sample Data db.addMd5HashValueDemo.insertMany([ {"UserName": "Adam", "UserPassword": "Adam123456"}, {"UserName": "Chris", "UserPassword": "Chris_121#"} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5cd6a4c66d78f205348bc619"), ...

Read More

Get maximum and minimum value in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 2K+ Views

To get the maximum and minimum values from a collection in MongoDB, use the $max and $min aggregation operators within a $group stage. This approach efficiently processes all documents to find the extreme values in a specified field. Syntax db.collection.aggregate([ { $group: { _id: null, MaximumValue: { $max: "$fieldName" }, ...

Read More

Merge two array fields in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 766 Views

To merge two array fields in MongoDB, use the $setUnion operator in an aggregation pipeline. This operator combines arrays and automatically removes duplicates while preserving unique values from both arrays. Syntax db.collection.aggregate([ { $project: { "MergedFieldName": { $setUnion: ["$arrayField1", "$arrayField2"] } ...

Read More
Showing 71–80 of 427 articles
« Prev 1 6 7 8 9 10 43 Next »
Advertisements