Found 6702 Articles for Database

Delete a collection from MongoDB with special characters?

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

428 Views

In order to delete the collection which has some special characters like _ or -, you need to use the following syntax −db.getCollection("yourCollectionName").drop();To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.createCollection("_personalInformation"); { "ok" : 1 } > db.getCollection('_personalInformation').insertOne({"ClientName":"Chris", "ClientCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9158bb4afe5c1d2279d6b2") } > db.getCollection('_personalInformation').insertOne({"ClientName":"Mike", "ClientCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9158c84afe5c1d2279d6b3") } > db.getCollection('_personalInformation').insertOne({"ClientName":"David", "ClientCountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9158d54afe5c1d2279d6b4") }Display all documents from a collection with the ... Read More

How to retrieve a random row or multiple random rows in MySQL?

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

150 Views

You can use RAND() method for this. To retrieve a random row, use the following syntaxSELECT *FROM yourTableName ORDER BY RAND() LIMIT yourIntegerNumber;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table gettingRandomRow    -> (    -> CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> CustomerName varchar(100)    -> ); Query OK, 0 rows affected (0.45 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into gettingRandomRow(CustomerName) values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into gettingRandomRow(CustomerName) values('Robert'); ... Read More

Removing _id element from PyMongo results?

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

495 Views

To remove the _id element, you can use the following syntax −db.yourCollectionName.find({}, {'_id': false}).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.removingidElementDemo.insertOne({"UserName":"John", ... "UserAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9153fd4afe5c1d2279d6ad") } > db.removingidElementDemo.insertOne({"UserName":"Carol", "UserAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9154084afe5c1d2279d6ae") } > db.removingidElementDemo.insertOne({"UserName":"David", "UserAge":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9154154afe5c1d2279d6af") } > db.removingidElementDemo.insertOne({"UserName":"Mike", "UserAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9154204afe5c1d2279d6b0") } > db.removingidElementDemo.insertOne({"UserName":"Chris", "UserAge":20}); {    "acknowledged" : true,   ... Read More

MySQL query to GROUP BY multiple columns

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

645 Views

You can use IF() to GROUP BY multiple columns. To understand the concept, let us create a table. The query to create a table is as followsmysql> create table MultipleGroupByDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> CustomerId int,    -> ProductName varchar(100)    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into MultipleGroupByDemo(CustomerId, ProductName) values(1000, 'Product-1'); Query OK, 1 row affected (0.20 sec) mysql> insert into MultipleGroupByDemo(CustomerId, ProductName) values(1001, 'Product-2'); Query OK, 1 row affected (0.18 ... Read More

How to count number of keys in a MongoDB document?

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

387 Views

There is no in-built function to count a number of keys in a document. In order to count a number of keys, you need to write some code.Let us create a collection with a document. The query to create a collection with a document is as follows −> db.numberofKeysInADocumentDemo.insertOne({    "UserName":"John", "UserAge":21, "UserEmailId":"john12@gmail.com", "UserCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9132584afe5c1d2279d6ac") }Display all documents from a collection with the help of find() method. The query is as follows −> db.numberofKeysInADocumentDemo.find().pretty();The following is the output −{    "_id" : ObjectId("5c9132584afe5c1d2279d6ac"),    "UserName" : "John",    "UserAge" : 21,   ... Read More

How can I sum columns across multiple tables in MySQL?

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

1K+ Views

To sum columns across multiple tables, use UNION ALL. To understand the concept, let us create first table. The query to create first table is as followsmysql> create table Products1    -> (    -> ProductId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> ProductName varchar(20),    -> ProductPrice int    -> ); Query OK, 0 rows affected (0.50 sec)Insert some records in the first table using insert command. The query is as follows −mysql> insert into Products1(ProductName, ProductPrice) values('Product-1', 100); Query OK, 1 row affected (0.22 sec) mysql> insert into Products1(ProductName, ProductPrice) values('Product-2', 200); Query OK, 1 row affected ... Read More

What is the equivalent of SQL “like” in MongoDB?

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

262 Views

You can use “$regex” operator to implement the equivalent of SQL ‘like’ in MongoDB. To implement it, let us create a collection with a document. The query to create a collection with a document is as follows −> db.sqlLikeDemo.insertOne({"UserName":"John Smith", "UserAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c912e124afe5c1d2279d6a5") } > db.sqlLikeDemo.insertOne({"UserName":"John Doe", "UserAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c912e264afe5c1d2279d6a6") } > db.sqlLikeDemo.insertOne({"UserName":"Chris Williams", "UserAge":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c912e404afe5c1d2279d6a7") } > db.sqlLikeDemo.insertOne({"UserName":"Robert Taylor", "UserAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c912e4d4afe5c1d2279d6a8") } > db.sqlLikeDemo.insertOne({"UserName":"John Brown", "UserAge":27}); {    "acknowledged" ... Read More

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

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

563 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

88 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

810 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

Advertisements