Found 6702 Articles for Database

How can I rename a collection in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

642 Views

To rename a collection in MongoDB, you can use renameCollection() method. The syntax is as follows −db.yourOldCollectionName.renameCollection('yourNewCollectionName');To understand the above syntax, let us list all the collections from database sample. The query is as follows −> use sample; switched to db sample > show collections;The following is the output −copyThisCollectionToSampleDatabaseDemo deleteDocuments deleteDocumentsDemo employee informationAboutDelete internalArraySizeDemo prettyDemo selectWhereInDemo sourceCollection updateInformation userInformationNow change collection name ‘informationAboutDelete’ to ‘deleteSomeInformation’. The query is as follows to change the collection name.> db.informationAboutDelete.renameCollection('deleteSomeInformation'); { "ok" : 1 }Here is the query to check the collection name has been renamed to 'deleteSomeInformation' −> show collections;The following is ... Read More

How to aggregate sum in MongoDB to get the total count?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

818 Views

To aggregate sum in MongoDB to get the total count, you can use the $sum operator. To understand the above concept, let us create a collection with the document −> db.aggregateSumDemo.insertOne({"CustomerName":"Larry", "Amount":140}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8baa0680f10143d8431e18") } > db.aggregateSumDemo.insertOne({"CustomerName":"Mike", "Amount":160}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8baa1380f10143d8431e19") } > db.aggregateSumDemo.insertOne({"CustomerName":"Sam", "Amount":300}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8baa1c80f10143d8431e1a") } > db.aggregateSumDemo.insertOne({"CustomerName":"David", "Amount":500}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8baa2580f10143d8431e1b") }Display all documents from a collection with the help of find() method. The query is as follows −> db.aggregateSumDemo.find().pretty();The following ... Read More

How do I delete blank rows in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

22K+ Views

Use the delete command to delete blank rows in MySQL.The syntax is as followsdelete from yourTableName where yourColumnName=' ' OR yourColumnName IS NULL;The above syntax will delete blank rows as well as NULL row.To understand the concept, let us create a table.The query to create a table is as followsmysql> create table deleteRowDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(20)    -> ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into deleteRowDemo(StudentName) values('John'); Query OK, 1 row affected ... Read More

Check the current number of connections to MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

702 Views

You can check the current number of connections to MongoDB with the help of the following syntax −var anyVariableName= db.serverStatus(); yourVariableName.connections;The second syntax is as follows −db.serverStatus().connections;To understand both the above syntaxes, let us see them one by one −Case 1 − The first query is as follows −> var checkCurrentNumberOfConnections = db.serverStatus() > checkCurrentNumberOfConnections.connections;The following is the output −{ "current" : 1, "available" : 999999, "totalCreated" : 1 }Case 2 − The second query is as follows −> db.serverStatus().connectionsThe following is the output −{ "current" : 1, "available" : 999999, "totalCreated" : 1 }

MongoDB Query for boolean field as “not true”

Smita Kapse
Updated on 30-Jul-2019 22:30:25

782 Views

You can use $ne(not equal) operator for this. The syntax is as follows −db.yourCollectionName.find({yourFieldName: {$ne: true}}).pretty();To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.queryForBooleanFieldsDemo.insertOne({"EmployeeName":"Larry", "EmployeeAge":24, "isOldEmployee":true}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8b7f7680f10143d8431e13") } > db.queryForBooleanFieldsDemo.insertOne({"EmployeeName":"Mike", "EmployeeAge":20, "isOldEmployee":false}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8b7f8680f10143d8431e14") } > db.queryForBooleanFieldsDemo.insertOne({"EmployeeName":"Sam", "EmployeeAge":23, "isOldEmployee":false}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8b7f9380f10143d8431e15") } > db.queryForBooleanFieldsDemo.insertOne({"EmployeeName":"David", "EmployeeAge":25, "isOldEmployee":true}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8b7fa280f10143d8431e16") } > db.queryForBooleanFieldsDemo.insertOne({"EmployeeName":"Carol", "EmployeeAge":27, "isOldEmployee":true}); ... Read More

Create a database in MySQL from Java?

George John
Updated on 30-Jul-2019 22:30:25

2K+ Views

The following is the code to create a database in MySQL from Java. We are creating the database with name, “Customer_Tracker_Database”import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class CreateDatabaseDemo {    public static void main(String[] args) {       Connection con=null;       Statement stmt=null;       String yourDatabaseName="Customer_Tracker_Database";       try {          con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test3?useSSL=false",          "root", "123456");          stmt = con.createStatement();          int status = stmt.executeUpdate("CREATE DATABASE "+yourDatabaseName);          if(status > 0) {             System.out.println("Database ... Read More

Get the returned record set order in MySQL IN clause?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

94 Views

For returned record set order, you need to use FIND_IN_SET() function. For an example, let us create a table.mysql> create table returnRecordSetOrderDemo    -> (    -> Id int,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (1.01 sec)Insert some records in the table using insert command.The query is as follows.mysql> insert into returnRecordSetOrderDemo values(100, 'John'); Query OK, 1 row affected (0.13 sec) mysql> insert into returnRecordSetOrderDemo values(130, 'Carol'); Query OK, 1 row affected (0.13 sec) mysql> insert into returnRecordSetOrderDemo values(103, 'Bob'); Query OK, 1 row affected (0.17 sec) mysql> insert into returnRecordSetOrderDemo values(134, 'Sam'); Query OK, ... Read More

MySQL: Testing connection with query?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

756 Views

Use any predefined function with select query or you can print some words with the select query in order to test connection with query.The syntax is as follows.SELECT yourValue;The select query with predefined function is as follows.The syntax is as follows.SELECT anyPredefinedFunctionName();Now you can implement the above syntax in order to test connection with query.Case 1 -The query is as follows.mysql> select "This is MySQL" as Display;The following is the output.+---------------+ | Display | +---------------+ | This is MySQL | +---------------+ 1 row in set (0.00 sec)Case 2 -The query is as follows.mysql> select ... Read More

How to select from MySQL table A that does not exist in table B?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

1K+ Views

You can use IN operator to select from one table that does not exist in another. To understand the above syntax, let us create a table.The first table name is A and second table name is B. The query to create a table is as followsmysql> create table A    -> (    -> Value int    -> ); Query OK, 0 rows affected (0.56 sec)Now you can insert some records in the table using insert command.The query is as followsmysql> insert into A values(10); Query OK, 1 row affected (0.23 sec) mysql> insert into A values(20); Query OK, 1 ... Read More

MongoDB Query to select records having a given key?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

111 Views

To select records having a given key, you can use $exists operator. The syntax is as follows −db.yourCollectionName.find( { yourFieldName: { $exists : true } } ).pretty();To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.selectRecordsHavingKeyDemo.insertOne({"StudentName":"John", "StudentAge":21, "StudentMathMarks":78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8b7be780f10143d8431e0f") } > db.selectRecordsHavingKeyDemo.insertOne({"StudentName":"Carol", "StudentMathMarks":89}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8b7bfc80f10143d8431e10") } > db.selectRecordsHavingKeyDemo.insertOne({"StudentName":"Sam", "StudentAge":26, "StudentMathMarks":89}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8b7c1280f10143d8431e11") } > db.selectRecordsHavingKeyDemo.insertOne({"StudentName":"Sam", "StudentMathMarks":98}); {    "acknowledged" : true, ... Read More

Advertisements