Ankith Reddy

Ankith Reddy

730 Articles Published

Articles by Ankith Reddy

Page 25 of 73

How to drop a numeric collection from MongoDB?

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

In MongoDB, numeric collection names cannot be accessed using standard dot notation. To drop a numeric collection, use the getCollection() method followed by drop(). Syntax db.getCollection("numericCollectionName").drop(); Create Sample Data First, create a numeric collection ? db.createCollection("2536464"); { "ok" : 1 } Now insert some documents in the collection ? db.getCollection("2536464").insertMany([ {"Record": 1}, {"Record": 2}, {"Record": 3} ]); { "acknowledged" : true, "insertedIds" ...

Read More

MongoDB equivalent of WHERE IN(1,2,...)?

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

The MongoDB equivalent of SQL's WHERE IN(1, 2, ...) clause is the $in operator. This operator allows you to match documents where a field value equals any value in a specified array. Syntax db.collectionName.find({ fieldName: { $in: [value1, value2, value3, ...] } }); Sample Data Let us create a collection with student documents ? db.whereInDemo.insertMany([ { "StudentName": "John", "StudentMathScore": 57 }, { "StudentName": "Larry", "StudentMathScore": 89 }, { "StudentName": "Chris", "StudentMathScore": 98 }, ...

Read More

How to query an Array[String] for a Regular Expression match?

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

To query an array of strings for a regular expression match in MongoDB, use the regular expression pattern directly in the find query. MongoDB automatically searches through array elements and matches any string that satisfies the regex pattern. Syntax db.collection.find({ arrayField: /regexPattern/ }) Create Sample Data db.queryArrayDemo.insertMany([ { "StudentFullName": [ "Carol Taylor", "Caroline Williams", ...

Read More

How to query on top N rows in MongoDB?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 3K+ Views

To query the top N rows in MongoDB, use the aggregation framework with $sort and $limit operators. This approach allows you to sort documents by any field and retrieve only the specified number of top results. Syntax db.collection.aggregate([ { $sort: { fieldName: 1 } }, // 1 for ascending, -1 for descending { $limit: N } // N = number of documents to return ]); Sample Data db.topNRowsDemo.insertMany([ ...

Read More

How can I use $elemMatch on first level array in MongoDB?

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

You can use $in operator instead of $elemMatch on first level array. For simple value matching in arrays, $in provides better performance and cleaner syntax. Syntax db.collection.find({ "arrayField": { $in: ["value1", "value2"] } }); Sample Data Let us create a collection with documents containing first−level arrays ? db.firstLevelArrayDemo.insertMany([ { "StudentName": "Chris", "StudentTechnicalSkills": ["MongoDB", "MySQL", "SQL Server"] }, { ...

Read More

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

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 867 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

How to terminate a MongoDB shell script earlier?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 579 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

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

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 257 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 return only value of a field in MongoDB?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 1K+ Views

To return only the values of a field in MongoDB, you can use projection with find() to select specific fields, or use forEach with an array to extract field values into a simple array format. Syntax // Method 1: Using projection db.collection.find({}, { "fieldName": 1, "_id": 0 }); // Method 2: Using forEach to extract values var output = []; db.collection.find().forEach(function(doc) { output.push(doc.fieldName); }); Sample Data db.returnOnlyValueOfFieldDemo.insertMany([ { "ClientName": "Larry" }, { "ClientName": "Chris" }, ...

Read More

How to find a record by _id in MongoDB?

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

To find a record by _id in MongoDB, use the find() method with the ObjectId as the query criteria. Every document in MongoDB has a unique _id field that serves as the primary key. Syntax db.collectionName.find({"_id": ObjectId("your_object_id")}); Sample Data Let us create a collection with sample customer documents ? db.findRecordByIdDemo.insertMany([ {"CustomerName": "Larry", "CustomerAge": 26}, {"CustomerName": "Bob", "CustomerAge": 20}, {"CustomerName": "Carol", "CustomerAge": 22}, {"CustomerName": "David", "CustomerAge": 24} ]); { "acknowledged": ...

Read More
Showing 241–250 of 730 articles
« Prev 1 23 24 25 26 27 73 Next »
Advertisements