MongoDB Articles

Page 90 of 111

How to convert from string to date data type in MongoDB?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 870 Views

To convert from string to date data type in MongoDB, you can use the ISODate() function within a script that iterates through documents and updates them. This is useful when you have date fields stored as strings that need to be converted to proper date objects. Syntax db.collection.find().forEach(function(doc){ doc.dateField = ISODate(doc.dateField); db.collection.save(doc); }); Create Sample Data Let us first create a collection with documents containing string date values ? db.stringToDateDataTypeDemo.insertMany([ {"CustomerName": "Carol", "ShippingDate": "2019-01-21"}, {"CustomerName": "Bob", ...

Read More

Find inside a hash MongoDB?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 325 Views

To find inside a hash (embedded document) in MongoDB, use dot notation to access nested fields. This allows you to query specific fields within embedded documents using the format "parentField.childField". Syntax db.collection.find({ "embeddedDocument.field": "value" }); Sample Data Let us create a collection with documents containing embedded ClientDetails ? db.hashDemo.insertMany([ { "ClientName": "Larry", "ClientAge": 23, "ClientDetails": { ...

Read More

Query MongoDB for a datetime value less than NOW?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 776 Views

To query MongoDB for datetime values less than the current time, use the $lte operator with new Date() which returns the current timestamp. Syntax db.collection.find({ "dateField": { $lte: new Date() } }); Sample Data db.dateTimeValueLessThanNowDemo.insertMany([ { "CustomerName": "Larry", "CustomerProductName": "Product-1", "ArrivalDate": new ISODate("2017-01-31") }, { ...

Read More

Adding new property to each document in a large MongoDB collection?

George John
George John
Updated on 15-Mar-2026 895 Views

To add a new property to each document in a large MongoDB collection, use the forEach() method combined with update() and $set operator. This approach iterates through all documents and adds the new field dynamically. Syntax db.collection.find().forEach(function(doc) { db.collection.update( {_id: doc._id}, {$set: {newProperty: computedValue}} ); }); Create Sample Data db.addingNewPropertyDemo.insertMany([ {"StudentName": "John", "StudentAge": 23, "CountryName": "US"}, {"StudentName": "David", "StudentAge": 21, "CountryName": ...

Read More

How to terminate a MongoDB shell script earlier?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 582 Views

To terminate a MongoDB shell script earlier, you can use the quit() or quit(1) command. This immediately exits the MongoDB shell and returns control to the operating system command prompt. Syntax quit() quit(1) Where quit() exits with status code 0 (success), and quit(1) exits with status code 1 (error). Sample Data Let us first create a collection with sample documents to demonstrate the quit functionality ? db.flightInformation.insertMany([ {"FlightName": "Flight-1", "ArrivalTime": new ISODate("2019-03-12")}, {"FlightName": "Flight-2", "ArrivalTime": new ISODate("2019-03-31")} ]); { ...

Read More

Renaming column name in a MongoDB collection?

George John
George John
Updated on 15-Mar-2026 809 Views

To rename a field (column) name in a MongoDB collection, use the $rename operator. This operator allows you to change field names across multiple documents in a single operation. Syntax db.collectionName.updateMany( {}, { $rename: { "oldFieldName": "newFieldName" } } ); Sample Data Let us create a collection with sample documents ? db.renamingColumnNameDemo.insertMany([ { "StudentName": "Larry", "Age": 23 }, { "StudentName": "Sam", "Age": 26 }, { "StudentName": "Robert", "Age": 27 } ]); ...

Read More

How to store query result (a single document) into a variable?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 598 Views

To store a query result (a single document) into a variable in MongoDB, use the var keyword with find().limit(1) to retrieve only one document. You can then access the stored result by referencing the variable name. Syntax var variableName = db.collectionName.find().limit(1); variableName; // Display the stored result Sample Data Let us first create a collection with sample documents ? db.storeQueryResultDemo.insertMany([ {"ClientName": "Chris", "ClientAge": 23}, {"ClientName": "Robert", "ClientAge": 21}, {"ClientName": "Sam", "ClientAge": 25}, {"ClientName": "Mike", "ClientAge": 26} ...

Read More

How to find two random documents in a MongoDB collection of 6?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 259 Views

To find two random documents from a MongoDB collection, use the $sample aggregation stage. This operator randomly selects a specified number of documents from the collection efficiently. Syntax db.collection.aggregate([ { $sample: { size: NumberOfDocuments } } ]); Sample Data Let us create a collection with 6 student documents ? db.twoRandomDocumentDemo.insertMany([ {"StudentId": 10}, {"StudentId": 100}, {"StudentId": 45}, {"StudentId": 55}, {"StudentId": 5}, {"StudentId": 7} ]); ...

Read More

How to check if field is a number in MongoDB?

George John
George John
Updated on 15-Mar-2026 599 Views

To check if a field is a number in MongoDB, use the $type operator with the value "number". This operator matches documents where the specified field contains numeric values (integers, doubles, or decimals). Syntax db.collectionName.find({fieldName: {$type: "number"}}); Sample Data Let us create a collection with documents containing different data types ? db.checkIfFieldIsNumberDemo.insertMany([ {"StudentName": "John", "StudentAge": 23}, {"StudentName": "Chris", "StudentMathScore": 98, "StudentCountryName": "US"}, {"StudentName": "Robert", "StudentCountryName": "AUS"}, {"StudentId": 101, "StudentName": "Larry", "StudentCountryName": "AUS"} ]); ...

Read More

Building Multiple Indexes at once in MongoDB?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 2K+ Views

To build multiple indexes at once in MongoDB, use the createIndexes() method and pass multiple index specifications in an array. This approach is more efficient than creating indexes one by one. Syntax db.collection.createIndexes([ { "field1": 1 }, { "field2": -1 }, { "field3": 1, "field4": 1 } ]); Example Let's create multiple single-field indexes on a collection ? db.multipleIndexesDemo.createIndexes([ {"First": 1}, {"Second": 1}, {"Third": 1}, ...

Read More
Showing 891–900 of 1,106 articles
« Prev 1 88 89 90 91 92 111 Next »
Advertisements