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 4 of 547
Which PHP function is used to delete an existing database?
PHP uses the mysql_query() function to delete a MySQL database. However, this function is deprecated since PHP 5.5.0 and removed in PHP 7.0.0. Modern PHP applications should use mysqli or PDO instead. Syntax (Legacy) bool mysql_query( sql, connection ); Parameters Parameter Description sql Required. SQL query to delete a MySQL database (DROP DATABASE command) connection Optional. If not specified, the last opened connection by mysql_connect will be used Modern Approach Using MySQLi Here's how to delete a database using the recommended MySQLi extension ? ...
Read MoreWrite an example to establish MySQL database connection using PHP script?
In PHP, you can establish a MySQL database connection using MySQLi or PDO extensions. The old mysql_connect() function is deprecated and should be avoided. Here are modern approaches to connect to MySQL database. Using MySQLi (Procedural) The MySQLi extension provides an interface to access MySQL databases − Using MySQLi (Object-Oriented) The object-oriented approach provides better code organization − Using PDO PDO (PHP Data Objects) provides a database-agnostic interface − Comparison Method Object-Oriented Prepared Statements Database Support ...
Read MoreWhich PHP function is used to disconnect from MySQL database connection?
PHP provides the mysql_close() function to disconnect from a MySQL database connection. This function takes a single parameter, which is a connection resource returned by the mysql_connect() function. Syntax bool mysql_close ( resource $link_identifier ); Here, if a resource is not specified, then the last opened database connection is closed. This function returns true if it closes the connection successfully, otherwise it returns false. Example The following example shows how to connect to and disconnect from a MySQL database − Important Note Deprecated Functions: The mysql_close() function ...
Read MoreHow to restart a NoSQL Database service like MongoDB?
To restart a MongoDB service, use the systemctl command on modern Linux systems. MongoDB runs as a system service called mongod that can be managed through systemd service management tools. Syntax sudo systemctl restart mongod sudo systemctl status mongod sudo systemctl enable mongod Method 1: Using systemctl (Recommended) Check the current status of MongoDB service ? sudo systemctl status mongod mongod.service - MongoDB Database Server Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled) Active: inactive (dead) Docs: https://docs.mongodb.org/manual Restart ...
Read MoreQuerying on an array of objects for specific nested documents with MongoDB?
To query on an array of objects for nested documents in MongoDB, use the find() method with the $elemMatch operator in projection to return only matching array elements. Syntax db.collection.find( {}, { arrayField: { $elemMatch: { condition } } } ); Sample Data db.demo763.insertOne({ _id: 1, ...
Read MoreMongoDB aggregation and projection?
MongoDB aggregation with projection allows you to transform and reshape documents by selecting specific fields, computing new values, and controlling the output structure. The $project stage in the aggregation pipeline passes documents with requested fields to the next stage. Syntax db.collection.aggregate([ { $project: { field1: 1, // Include field field2: 0, // Exclude field ...
Read MoreCheck for duplicates in an array in MongoDB?
To check for duplicates in an array in MongoDB, use the $unwind and $group operators within an aggregation pipeline to identify array elements that appear more than once in the same document. Syntax db.collection.aggregate([ { $project: { arrayField: 1 } }, { $unwind: "$arrayField" }, { $group: { _id: { _id: "$_id", value: "$arrayField" }, count: { $sum: 1 } } }, { $match: { count: { $gt: 1 } } }, { $group: { _id: "$_id._id", ...
Read MoreSort MongoDB field which contains both integer and decimal values?
To sort a MongoDB field containing both integer and decimal values, use the sort() method. MongoDB automatically handles mixed numeric types (integers and decimals) in the same field and sorts them by their numeric value. Syntax db.collection.find().sort({fieldName: 1}); // 1 for ascending, -1 for descending Sample Data db.demo755.insertMany([ {"Value": 10}, {"Value": 10.5}, {"Value": 9.5}, {"Value": 12.5}, {"Value": 11.5}, {"Value": 6} ]); { ...
Read MoreHow to convert birth date records to age with MongoDB
To convert birth date records to age in MongoDB, use the $dateDiff operator or calculate the difference between the current date and birth date using $subtract and $divide operators in an aggregation pipeline. Syntax db.collection.aggregate([ { $project: { age: { $divide: [ ...
Read MoreFilter specific values from a MongoDB document
To filter specific values from a MongoDB document, use the $filter operator in aggregation pipelines. This operator allows you to select array elements that match specific conditions and return only those elements. Syntax { $filter: { input: "$arrayField", as: "variable", cond: { condition } } } Sample Data db.demo751.insertOne({ _id: 101, details: [ ...
Read More