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
Articles by Samual Sam
Page 58 of 151
Does Mongo shell treats numbers as float by default.? How can we work it around explicitly?
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 MoreGet output of MongoDB shell script?
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 MoreMongoDB query to match each element in a documents array to a condition?
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 MoreHow to select only numeric strings in MongoDB?
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 MoreHow to find all objects created before specified Date in MongoDB?
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 MoreHow can I save new Date() in MongoDB?
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 MoreCount distinct value in MongoDB?
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 MoreGet index of given element in array field in MongoDB?
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 MoreUnset 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 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 More