https://www.w3resource.com/sql/sql-table.php
https://www.w3resource.com/sql/sample-database-of-sql-in-mysql-format.txt
Logon as userAdmin and create querydb database and user admin.
mongo mongodb://userAdmin:dfuer56pass34sm@adown-inf --authenticationDatabase 'admin'
use querydb
db.createUser( { user: 'admin', pwd: 'secret', roles: [ { role: 'root', db: 'admin' } ] } );
cp template/env.rc
vi env.rc
Variable | Description | Sample value |
---|---|---|
DELIM | Delimiter used in input text files | ; |
MONGODIR | Subdirectory for MongoDB input text files | mongotxt |
URI | URI string to connect to MongoDB instance | "mongodb://admin:secret@boreal-inf/querydb" |
DATABASE | MongoDB database | querydb |
LOGFILE | File to keep debug log lines | /tmp/log/e.log |
./importmongo.sh
SELECT cust_name,cust_city FROM customer;
db.customer.find(
undefined,
{"cust_name": 1,"cust_code": 1}
)
SELECT * FROM CUSTOMER WHERE GRADE >= 2;
db.customer.find(
{"grade": {"$gte": 2}}
)
Write a SQL statement to display all customers, who are either belongs to the city New York or had a grade above 2
SELECT * FROM customer WHERE cust_city = 'New York' OR grade>2;
db.customer.find(
{"$or": [{ "grade": { "$gt": 2 }},{ "cust_city": { "$eq": "New York" }}]}
)
Write a SQL statement to display either those orders which are not issued on date 2012-09-10 and issued by the salesman whose ID is 5005 and below or those orders which purchase amount is 1000.00 and below.
SELECT * FROM orders WHERE NOT ((ord_date ='2012-09-10' AND agent_code>5005) OR ord_amount>1000.00);
(MongoDB Query language does not have top level not operator. So expression NOT ((expr1) OR (expr2)) is replaced with { $nor: [expr1, expr2] }
db.orders.find(
{"$nor": [{ "$and": [ { "ord_date": { "$eq": "2012-09-10" } }, { "agent_code": { "$gt": 5005 } } ]},
{ "ord_amount": { "$gt": 1000 }}]}
)
SELECT DISTINCT AGENT_CODE FROM orders;
db.orders.distinct('agent_code')
SELECT COUNT(DISTINCT AGENT_CODE) FROM orders;
db.orders.distinct('agent_code').length
SELECT SUM (ORD_AMOUNT) FROM orders;
db.orders.aggregate([{$group: {_id: ObjectId(),total: {$sum: "$ord_amount"}}}])
SELECT AVG (ORD_AMOUNT) FROM orders;
db.orders.aggregate([{$group: {_id: ObjectId(),avg: {$avg: "$ord_amount"}}}])