Dpk_SQL, MongoDB queries
Dpk_SQL, MongoDB queries
😮
Stack// all syntax, variable name 'as you wish' (i.e SELECT, selEct, MOviES, MOVIES, movies,
NamE, RaTing) can use anywhere
1. Open MySQL prompt
mysql -u root -p // command
Enter password: @Deepak******
........
2. To see the databases (It is necessary to add ';' at the and of all commands)
show databases;
........
SELECT First_Name, Networth FROM Actors WHERE Networth > 140 OR Networth < 140;
SELECT * FROM Actors WHERE Gender LIKE "%M%" ORDER BY Gender, Networth ASC; //
Pehle Gender k base pe fir Networth k base pe
UPDATE Actors SET Networth = 130 WHERE First_Name = "Sunny" AND Last_Name =
“Devol”;
2. Non-Aggregated Query:
● Does not use aggregate functions.
● Retrieves individual rows or performs simple filtering and sorting operations.
2. Show databases
show databases; /or/
show dbs;
3. Use database
use database_name
4. Show collections
show collections
show tables
# If mongodb sees that there is no valid collection added in the database, and the db is empty,
it doesn’t list it.
7. Create a collection
db.createCollection(“collection_name”)
# Two documents of the same collection can possess different types of properties.
17. Projections (select specific key-value pair. i.e, select id, name from students)
db.collection_name.find ( { filter1: value1…}, { property1: true, property2: true…} )
db.weather_data.find ( { type:'FM-13' }, { position: true, visibility: true } ) // or
db.weather_data.find ( { }, { position: true, visibility: true } ) // first argument is necessary
updateMany: all records which are complying to the filtration criteria will be update
21. Get distinct value of a particular key (all unique value of a key)
db.weather_data.distinct('key')
(for string)
db.collection_name.createIndex({property_name: “text”})
……….……….……….
# Before we created our index on the elevation property we were actually reading all the docs
i.e, approx 9991 docs, to fetch all the docs with elevation less than 5000.
# But after creating the index this number will decrease drastically.
……….……….……….
28. Search for a single word (word should be of index. So, make it index first)
db.collection_name.find ( { $text : { $search : “text_name” } } )
EXAMPLE
Let, for ‘articles’ collection document is:
{ _id: 1, subject: “coffee”, author: “xyz”, views: 50 },
{ _id: 1, subject: “coffee and tea”, author: “xyz”, views: 50 }