Found 6705 Articles for Database

Is it possible to use variable for collection name using PyMongo?

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

682 Views

PyMongo is a Python distribution containing tools for working with MongoDB. Use the following syntax to use a variable for collection name −var yourVariableName="yourCollectionName"; db[storeCollectionName].yourOperationName;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. We have used variable for collection name −> var storeCollectionName="new_Collection"; > db[storeCollectionName].insertOne({"UserName":"John", "UserAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c912aea4afe5c1d2279d6a0") } > db[storeCollectionName].insertOne({"UserName":"Carol", "UserAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c912af54afe5c1d2279d6a1") } > db[storeCollectionName].insertOne({"UserName":"Mike", "UserAge":27}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c912afe4afe5c1d2279d6a2") }Display ... Read More

MySQL replication: temporarily prevent specific SQL statements replicating to the slaves?

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

139 Views

To achieve this, you need to set sql_log_bin to 0. To understand the concept, let us create a table. The query to create a table is as followsmysql> create table SQLStatementsDemo    -> (    -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> UserName varchar(20)    -> ); Query OK, 0 rows affected (0.79 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into SQLStatementsDemo(UserName) values('John'); Query OK, 1 row affected (0.18 sec) mysql> insert into SQLStatementsDemo(UserName) values('Carol'); Query OK, 1 row affected (0.14 sec) mysql> insert into SQLStatementsDemo(UserName) values('Bob'); Query ... Read More

Get database data size in MongoDB?

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

928 Views

To get the database data size in MongoDB, you can use stats() method. The syntax is as follows −db.stats();Let us use the database with the name ‘test’.Now, check the current database with the help of the following query −> db;The following is the output −testHere is the query to get database data size in MongoDB −> db.stats()The following is the output −{    "db" : "test",    "collections" : 114,    "views" : 0,    "objects" : 391,    "avgObjSize" : 83.0076726342711,    "dataSize" : 32456,    "storageSize" : 3211264,    "numExtents" : 0,    "indexes" : 120,    "indexSize" ... Read More

Find a document with ObjectID in MongoDB?

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

323 Views

To find a document with Objectid in MongoDB, use the following syntax −db.yourCollectionName.find({"_id":ObjectId("yourObjectIdValue")}).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.findDocumentWithObjectIdDemo.insertOne({"UserName":"Larry"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c90e4384afe5c1d2279d69b") } > db.findDocumentWithObjectIdDemo.insertOne({"UserName":"Mike", "UserAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c90e4444afe5c1d2279d69c") } > db.findDocumentWithObjectIdDemo.insertOne({"UserName":"Carol", "UserAge":26, "UserHobby":["Learning", "Photography"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c90e4704afe5c1d2279d69d") }Display all documents from a collection with the help of find() method. The query is as follows −> db.findDocumentWithObjectIdDemo.find().pretty();The following is the output ... Read More

Does deleting row from view delete row from base table in MySQL?

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

2K+ Views

Yes, deleting row from view delete row from base table. Let us understand this by creating a new table. The query to create a table is as followsmysql> create table deleteFromBaseTableDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(20) -> ); Query OK, 0 rows affected (0.83 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into deleteFromBaseTableDemo(Name) values('John'); Query OK, 1 row affected (0.18 sec) mysql> insert into deleteFromBaseTableDemo(Name) values('Carol'); Query OK, 1 row affected ... Read More

Get distinct values and count them in MySQL

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

504 Views

To get distinct values and count them, you can use GROUP BY clause.The syntax is as followsselect yourColumnName, count(*) as anyAliasName from yourTableName group by yourColumnName;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table GroupByAndCountDemo    -> (    -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> ClientName varchar(100)    -> ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into GroupByAndCountDemo(ClientName) values('John'); Query OK, 1 row affected (0.18 sec) mysql> insert ... Read More

Delete one row and reorder the others with the correct ID in MySQL?

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

601 Views

To understand the concept, let us first create a table. The query to create a table is as followsmysql> create table ReorderSortDemo -> ( -> UserId int -> ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into ReorderSortDemo values(14); Query OK, 1 row affected (0.13 sec) mysql> insert into ReorderSortDemo values(4); Query OK, 1 row affected (0.10 sec) mysql> insert into ReorderSortDemo values(6); Query OK, 1 row affected (0.11 sec) mysql> insert into ReorderSortDemo values(3); Query ... Read More

MySQL query to combine two columns in a single column?

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

2K+ Views

You can use COALESCE() function for this. In the COALESCE() function, it returns the first NON NULL value from the column. To understand the concept, let us first create a demo tablemysql> create table combineTwoColumnsDemo    -> (    -> UserId int,    -> UserName varchar(20),    -> UserAge int    -> ); Query OK, 0 rows affected (1.12 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into combineTwoColumnsDemo values(101, 'John', 23); Query OK, 1 row affected (0.16 sec) mysql> insert into combineTwoColumnsDemo values(102, 'Carol', 20); Query OK, 1 row affected (0.14 ... Read More

Avoid duplicate entries in MongoDB?

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

1K+ Views

To avoid duplicate entries in MongoDB, you can use createIndex(). The syntax is as follows −db.yourCollectionName.createIndex({"yourFieldName":1}, {unique:true});Let us implement the above syntax. The query to avoid duplicate entries in MongoDB is a follows −> db.avoidDuplicateEntriesDemo.createIndex({"UserName":1}, {unique:true}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }Now insert some records in the above collection. The query to insert record is as follows −> db.avoidDuplicateEntriesDemo.insertOne({"UserName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c90e1824afe5c1d2279d697") }Here is the error whenever you try to insert the same record once again −> db.avoidDuplicateEntriesDemo.insertOne({"UserName":"John"}); 2019-03-19T18:03:08.465+0530 E QUERY [js] ... Read More

How to determine if a value appears in a GROUP BY group in MySQL?

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

1K+ Views

You can use aggregate function SUM() along with IF to determine if a value appears in a GROUP BY group.Let us first create a demo tablemysql> create table GroupbygroupDemo -> ( -> UserId int, -> UserName varchar(20) -> ); Query OK, 0 rows affected (1.48 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into GroupbygroupDemo values(10, 'John'); Query OK, 1 row affected (0.14 sec) mysql> insert into GroupbygroupDemo values(10, 'Carol'); Query OK, 1 row affected (0.08 sec) mysql> insert into ... Read More

Advertisements