Found 6705 Articles for Database

Is it possible to write to MongoDB console in JavaScript execution?

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

118 Views

To write on console, you need to use print() method. The syntax is as follows −print(“yourString”);To display objects, you can use printjson(). The syntax is as follows −printjson(yourObjectName);Let us implement both the functions. The first query is as follows to display something −> print("Welcome to MongoDB Console");The following is the output on a console −Welcome to MongoDB ConsoleLet us create an object. The query is as follows −>studentInformation={"StudentName":"John", "StudentAge":24, "StudentTechnicalSkills":["C", "C++", "Java", "MongoDB", "MySQL"]}; {    "StudentName" : "John",    "StudentAge" : 24,    "StudentTechnicalSkills" : [       "C",       "C++",       "Java",     ... Read More

Clone a collection in MongoDB?

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

656 Views

To clone a collection in MongoDB, you can use the forEach() method. Let us first create a collection with a document.The query to create a collection with a document is as follows −> db.studentInformation.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8bc15780f10143d8431e21") } > db.studentInformation.insertOne({"StudentName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8bc15e80f10143d8431e22") } > db.studentInformation.insertOne({"StudentName":"James"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8bc17380f10143d8431e23") }Display all documents from a collection with the help of find() method. The query is as follows −> db.studentInformation.find().pretty();The following is the output −{ "_id" : ObjectId("5c8bc15780f10143d8431e21"), "StudentName" : "Chris" } { "_id" ... Read More

How to get element with max id in MongoDB?

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

1K+ Views

To get the element with a max id, you can use the find() method. To understand the above concept, let us create a collection with the document. The query is as follows −> db.getElementWithMaxIdDemo.insertOne({"Name":"John", "Age":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8bbce480f10143d8431e1c") } > db.getElementWithMaxIdDemo.insertOne({"Name":"Larry", "Age":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8bbcec80f10143d8431e1d") } > db.getElementWithMaxIdDemo.insertOne({"Name":"David", "Age":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8bbcf580f10143d8431e1e") } > db.getElementWithMaxIdDemo.insertOne({"Name":"Chris", "Age":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8bbcfe80f10143d8431e1f") } > db.getElementWithMaxIdDemo.insertOne({"Name":"Robert", "Age":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8bbd0880f10143d8431e20") }Display all documents from ... Read More

How can I rename a collection in MongoDB?

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

782 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

957 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

24K+ 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

821 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

971 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

153 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

Advertisements