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
Database Articles
Page 19 of 547
How to clear a MongoDB database?
To clear a MongoDB database completely, use the dropDatabase() method. This permanently removes the database and all its collections, documents, and indexes. Syntax use yourDatabaseName; db.dropDatabase(); Example: Clear a MongoDB Database First, let us show all existing databases ? show dbs MyDB 0.000GB admin 0.000GB config 0.000GB local 0.000GB onlinecustomertracker 0.000GB test 0.006GB Now, switch to the database you want to delete and drop it ? use onlinecustomertracker db.dropDatabase(); switched to ...
Read MoreUsing regex in MongoDB findOne()
The findOne() method returns the first document that matches the specified query criteria in a MongoDB collection. When combined with regular expressions, it provides powerful text matching capabilities for pattern-based searches. Syntax db.collection.findOne({field: {$regex: /pattern/flags}}) Sample Data Let's create a collection with sample documents ? db.demo655.insertMany([ {subject: "MySQL"}, {subject: "MongoDB"}, {subject: "Java"} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5ea050254deddd72997713cc"), ...
Read MoreIn MongoDB, is using $in search faster than multiple single searches?
Yes, using $in is significantly faster than multiple single searches. The $in operator performs a single database query to match multiple values, while multiple single searches require separate round trips to the database, increasing network overhead and execution time. Syntax db.collection.find({ field: { $in: [value1, value2, value3] } }); Sample Data db.demo653.insertMany([ { subject: "MySQL" }, { subject: "MongoDB" }, { subject: "Java" }, { subject: "C" }, { ...
Read MoreSort the MongoDB documents in ascending order with aggregation?
To sort MongoDB documents in ascending order with aggregation, use the $sort stage in the aggregation pipeline with field: 1 (ascending) or field: -1 (descending). Syntax db.collection.aggregate([ { $sort: { fieldName: 1 } } // 1 for ascending, -1 for descending ]); Sample Data db.demo652.insertMany([ { value: 10, "details": [ { ...
Read MoreAvoid MongoDB performance issues while using regex
To avoid MongoDB performance issues while using regex, create appropriate text indexes on the fields you'll be searching. Text indexes enable efficient text search operations and significantly improve query performance. Syntax db.collection.createIndex({"field1": "text", "field2": "text"}); db.collection.find({"field": {"$regex": "pattern", "$options": "i"}}); Create Text Index First, create a text index on the fields you'll be searching ? db.demo531.createIndex({"CountryName": "text", "Name": "text"}); { "createdCollectionAutomatically": true, "numIndexesBefore": 1, "numIndexesAfter": 2, "ok": 1 } Sample Data ...
Read MoreHow to use ORDERBY in MongoDB if there are possible null values?
When sorting documents containing null values in MongoDB, use the sort() method. MongoDB treats null values as the smallest value, so they appear first in ascending order and last in descending order. Note − Starting in MongoDB v3.2, the $orderby operator is deprecated in the mongo shell. Use cursor.sort() instead. Syntax db.collection.find().sort({ "fieldName": 1 }); // Ascending (nulls first) db.collection.find().sort({ "fieldName": -1 }); // Descending (nulls last) db.collection.aggregate([ { $match: { "fieldName": { ...
Read MoreMongoDB query to group by _id
To group by _id in MongoDB, use the $group stage in an aggregation pipeline. This allows you to group documents by the _id field or set _id: null to group all documents together. Syntax db.collection.aggregate([ { $group: { _id: "$fieldName", aggregatedField: { $operator: "expression" } } } ]); ...
Read MoreMatching MongoDB collection items by id?
To match collection items by id, use the $in operator in MongoDB. This allows you to query multiple documents by their specific ObjectId values in a single operation. Syntax db.collection.find({ _id: { $in: [ObjectId("id1"), ObjectId("id2"), ObjectId("id3")] } }); Create Sample Data db.demo528.insertMany([ {"Name": "Chris", "Age": 21}, {"Name": "Bob", "Age": 22}, {"Name": "David", "Age": 20} ]); { "acknowledged": true, "insertedIds": [ ...
Read MorePerform Group in MongoDB and sum the price record of documents
To sum price records across all documents in MongoDB, use the $group stage in an aggregation pipeline with the $sum operator. Set _id: null to group all documents together. Syntax db.collection.aggregate([ { $group: { _id: null, totalFieldName: { $sum: "$fieldName" } } } ]); Sample Data ...
Read MoreUpdating an array with $push in MongoDB
To update an array with $push in MongoDB, use the updateOne() method. The $push operator adds elements to an array field, and when combined with the positional operator $, it can target specific array elements. Syntax db.collection.updateOne( { "arrayField": { "$elemMatch": { "field": "value" } } }, { "$push": { "arrayField.$.targetField": newElement } } ); Sample Data Let us create a collection with documents ? db.demo526.insertOne({ "CountryName": "US", "TeacherName": "Bob", "StudentInformation": [ ...
Read More