Samual Sam

Samual Sam

1,507 Articles Published

Articles by Samual Sam

Page 58 of 151

Does Mongo shell treats numbers as float by default.? How can we work it around explicitly?

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

Yes, MongoDB shell treats numbers as double (float) by default. To work with integers or other specific number types, you need to explicitly specify the type using MongoDB's type constructors like NumberInt(), NumberLong(), or NumberDecimal(). Syntax NumberInt("value") // 32-bit integer NumberLong("value") // 64-bit integer NumberDecimal("value") // 128-bit decimal Example: Creating Integer Array Let's create an array with explicit integer values instead of floats ? var integerArrayDemo = [ NumberInt("50"), NumberInt("60"), ...

Read More

Get output of MongoDB shell script?

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

You can use printjson() or print() to get output of MongoDB shell script. The printjson() method displays JavaScript objects in a formatted JSON structure, while print() outputs plain text. Syntax printjson(object); print(value); Create Sample Data Following is the query to create an array of objects ? var studentDetails = [ {"StudentName": "John", "StudentAge": 21}, {"StudentName": "Carol", "StudentAge": 24}, {"StudentName": "David", "StudentAge": 25} ]; Example 1: Using printjson() Following is the query to get the output of MongoDB ...

Read More

MongoDB query to match each element in a documents array to a condition?

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

To match each element in a document's array to a condition in MongoDB, use the $where operator with JavaScript's every() method. This approach allows you to verify that all array elements satisfy a specific condition. Syntax db.collection.find({ $where: "return this.arrayField.every(function(element) { return (condition); })" }); Sample Data db.arrayConditionDemo.insertMany([ {"Name": "John", "Marks": [40, 43, 45]}, {"Name": "Mike", "Marks": [45]}, {"Name": "Chris", "Marks": [43, 45, ...

Read More

How to select only numeric strings in MongoDB?

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

To select only numeric strings in MongoDB, use regular expressions with the pattern /^\d+$/ which matches strings containing only digits from start to end. Syntax db.collection.find({ "field": /^\d+$/ }); Sample Data db.selectOnlyNumericDemo.insertMany([ { "UserId": "User101" }, { "UserId": "102" }, { "UserId": "User103" }, { "UserId": "104" } ]); { "acknowledged": true, "insertedIds": [ ObjectId("5cbdb711de8cc557214c0e16"), ...

Read More

How to find all objects created before specified Date in MongoDB?

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

To find all objects created before a specified date in MongoDB, use the $lt (less than) operator with ISODate() to compare date values in your query condition. Syntax db.collection.find({ "dateField": { $lt: ISODate("YYYY-MM-DD") } }); Sample Data db.beforeSpecifyDateDemo.insertMany([ { "UserLoginDate": new ISODate('2016-03-21') }, { "UserLoginDate": new ISODate('2016-05-11') }, { "UserLoginDate": new ISODate('2017-01-31') }, { "UserLoginDate": new ISODate('2018-05-15') }, { "UserLoginDate": new ISODate('2019-04-01') } ]); { ...

Read More

How can I save new Date() in MongoDB?

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

To save new Date() in MongoDB, use new ISODate() or new Date() for date and datetime values. MongoDB automatically stores dates in BSON Date format, which is represented as ISODate in the shell. Syntax // Using ISODate db.collection.insertOne({ "field": new ISODate("YYYY-MM-DD HH:MM:SS") }); // Using Date db.collection.insertOne({ "field": new Date("YYYY-MM-DD HH:MM:SS") }); Create Sample Data Insert documents with date fields using new ISODate() ? db.saveDateDemo.insertMany([ { "UserName": "John", ...

Read More

Count distinct value in MongoDB?

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

To count distinct values in MongoDB, use the distinct() method combined with the .length property. This returns the number of unique values for a specified field across all documents in a collection. Syntax db.collectionName.distinct("fieldName").length; Create Sample Data Let us create a collection with documents that contain duplicate values ? db.countDistinctDemo.insertMany([ {"StudentName": "John"}, {"StudentName": "Chris"}, {"StudentName": "Chris"}, {"StudentName": "Carol"}, {"StudentName": "David"}, {"StudentName": "Carol"} ]); { ...

Read More

Get index of given element in array field in MongoDB?

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

To get the index of a given element in an array field in MongoDB, use the $indexOfArray operator within an aggregation pipeline. This operator returns the zero-based index of the first occurrence of the specified element in the array. Syntax db.collection.aggregate([ { $project: { "fieldName": { $indexOfArray: [ "$arrayField", "searchElement" ] ...

Read More

Unset an attribute from a single array element in MongoDB?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 235 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

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

Samual Sam
Samual Sam
Updated on 15-Mar-2026 247 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
Showing 571–580 of 1,507 articles
« Prev 1 56 57 58 59 60 151 Next »
Advertisements