Monday, 30 November 2015

MongoDB Database Commands with Examples

mongoDB Database Commands with Examples

Question: Expalin the Basic terminology for MongoDB?
RDBMS MongoDB
Database Database
Table Collection
Tuple/Row Document
column Field
Table Join Embedded Documents
Primary Key Primary Key (Default key _id provided by mongodb itself)
Mysqld/Oracle mongod
mysql/sqlplus mongo

Note: # used for commenting.

Question: Create a New database?
use mydb #switched to db mydb



Question: Check which database you are currently using?
use



Question: Display the List of databases?
show dbs #All database will display which have atleast 1 document.



Question: Delete the current database?
db.dropDatabase() #Delete the current used database.



Question: How to create collection for a database?
db.createCollection("mycollection") #create a collection for current selected database.



Question: Display the List of collections in database?
show collections #All collection will display for current database.



Question: How to drop the collection?
db.mycollection.drop() #mycollection collection will be deleted.



Question: How to delete all the records from mongodb ?
db.collection.remove();



Question: How to delete all the records with condition?
db.collection.remove({uid=111});

Will delete all the record where uid=1111


Question: How to insert data(Know as document) into collection?
db.mycollection.insert({
   _id: ObjectId(7df78ad89765),
   title: 'MongoDB Overview',    
   by: 'Web technology',   
   tags: ['mongodb', 'database', 'NoSQL'],   
})
#Single document is added in collection "mycollection" .



Question: How to add multiple document into collection in single command?
db.mycollection.insert({
   {   
   title: 'MongoDB Overview',    
   by: 'Web technology',   
   tags: ['mongodb', 'database', 'NoSQL'],   
   },
 
   {
   title: 'MongoDB Overview2',    
   by: 'Web technology experts notes',   
   tags: ['mongodb', 'database', 'NoSQL' ,'Multiple Record'],   
       
   }
])



Question: What is command for search a document? Give Example?
find() is used to search. For Example

db.mycollection.find()#Search the one document in un structured way .



Question: How to search a document in pretty way (structured way) ? Give Example?
pretty() is used to search in pretty way. For Example

db.mycollection.find({"by":"Web technology"}).pretty()#Search the one document in structured way .



Question: How to search a document with "and condition"?
db.mycollection.find({"by":"Web technology",{"title": "MongoDB Overview"}}).pretty()#Search the one document in structured way .



Question: How to list first 10 document?
db.mycollection.find({"by":"Web technology",{"title": "MongoDB Overview"}}).limit(10).pretty()#Search the 10 document in structured way .



Question: How to get 2nd document?
db.mycollection.find({"by":"Web technology",{"title": "MongoDB Overview"}}).limit(1).skip(1).pretty()#Search the 10 document in structured way .



Question: How to list document with title ascending order?
db.mycollection.find({"by":"Web technology",{"title": "MongoDB Overview"}}).sort({"title":1}).pretty()#Search the in title ascending order.



Question: How to search document in title descending order?
db.mycollection.find({"by":"Web technology",{"title": "MongoDB Overview"}}).sort({"title":-1}).pretty()#Search the in title descending order. 



Question: How to Add indexing?
db.mycollection.ensureIndex({"title":1,"description":-1})#title in ascending order and description in descending order.To create index in descending order you need to use -1. 



Question: How to search a document with "OR condition"?
db.mycollection.find({"by":"Web technology",$or[{"title": "MongoDB Overview"}]}).pretty()#Search the one document in structured way .



Question: How to update a document?
db.mycollection.update({'title':'MongoDB Overview'},{$set:{'title':'MongoDB text'}})#update "MongoDB Overview" with "MongoDB text " .



Question: How to delete a document?
db.mycollection.remove({'title':'MongoDB Overview'})#Delete the record where document is 'MongoDB Overview' .




Find all records
db.mycollection.find();
Display all the records in this collection.



Find all records and display in pretty way
db.mycollection.find().pretty();
Display all the records in this collection but presentable way.



Find all records with single condition (Age: 29)
db.mycollection.find({age:29});
Display all the records where age=29.



Find all records with multiple AND condition (Age: 29, Number:17)
db.mycollection.find({age:29, number:17});
Display all the records where age=29 and number=17



Find all records with multiple OR condition (Age: 29 OR Number:17)
db.mycollection.find({$or:[{age:29},{number:17}]});
Display all the records where age=29 OR number=17 (each of one).



Find all records with multiple OR condition (Age>28 OR Number:17)
db.mycollection.find({$or:[{age:{$gt:28}},{number:17}]});
Display all the records where age>29 and number=17



Find all records and display and one display column (name)
db.mycollection.find({},{name:1}).pretty();
Display all the name in this collections.



Find all records and display and two display column (name and number)
db.mycollection.find({},{name:1,number:1}).pretty();
Display all the name and number in this collections.



Limit the number of record
db.mycollection.find().limit(3).pretty();
Display only 3 records.



Display all the records except 1,2,3
db.mycollection.find().skip(3).pretty();
Skip first 3 records.


Saturday, 28 November 2015

PayPal Express Checkout Working Flow - In Simple Steps with Clean Example


PayPal Express Checkout Working Flow
PayPal Express Checkout is a powerful API-based solution that can be integrated into any merchant website. With PayPal Express Checkout API, customer leave the website and complete the transaction in http://paypal.com, where customer can pay with paypal.com account OR with credit card / Debit card. When start processing, Paypal gives a TOKEN key which is used to charge money from customer OR get the Payment Details OR Customer Shipping/Billing details. Developer should save this token key for future use.


Express Checkout have Simple Seven Steps. Start from initializing the token and end with success page after payment.


Customer makes their payment and completes their order on your website. This enables tighter integration with your website and order management processes. It can be more efficient for PayPal users and may facilitate sales.



Following are 7 Steps of express Checkout
Step 1: Get Token API In this case we need to send an API call to Paypal Server with item detail, receiver email etc.
Request Parameter Example
Array
(
    [USER] => PAYPAL_API_EMAIL
    [PWD] => PAYPAL_API_PASSWORD
    [SIGNATURE] => PAYPAL_API_SIGNATURE
    [VERSION] => 95
    [ip] => 127.0.0.1
    [METHOD] => SetExpressCheckout
    [returnUrl] => http://example.com/order/success //customer will return this URL, if paid
    [cancelUrl] => http://example.com/order/failed //customer will return this URL, if not paid
    [L_PAYMENTREQUEST_0_NAME0] => Om Belt - Black
    [L_PAYMENTREQUEST_0_DESC0] => Obscure Belts
    [L_PAYMENTREQUEST_0_AMT0] => 82.92
    [L_PAYMENTREQUEST_0_QTY0] => 1
    [PAYMENTREQUEST_0_CURRENCYCODE] => USD
    [PAYMENTREQUEST_0_SHIPPINGAMT] => 5.15
    [PAYMENTREQUEST_0_AMT] => 88.07
    [PAYMENTREQUEST_0_ITEMAMT] => 82.92
    [PAYMENTREQUEST_0_PAYMENTACTION] => Order
    [PAYMENTREQUEST_0_SELLERPAYPALACCOUNTID] => receiver_email_address;//payment will receive by this paypal email
    [PAYMENTREQUEST_0_PAYMENTREQUESTID] => ORDER_UNIQUE_ID
)

Step 2: Get Token From API Response After calling above API, we will get below response
Array
(
    [TOKEN] => EC-06N33746DC1035058
    [TIMESTAMP] => 2014-08-11T11:40:52Z
    [CORRELATIONID] => dcda5dabca4a7
    [ACK] => Success
    [VERSION] => 95
    [BUILD] => 12301660
)
Now, Store the TOKEN will be used in future


Step 3: Send Customer to Paypal After getting success, we will send customer to following URL with appending token key
https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&useraction=commit&token=EC-06N33746DC1035058
Here User will see Cart detail and can pay after login.

Step 4: Order Confirmation Page After login successfully, Customer will move to Order Confirmation page with "Pay Now" Button
(Here user will see the cart detail and can pay)

Step 5: Get Order Detail When Customer paid successfully, customer will return to returnUrl (see step1), In return URL, TOKEN and PayerID will be appended automatically.
From TOKEN, we will get to know for what order user return.
From PayerID, We will get to know user is ready to pay us and also we used this Id to charge him.

Call following API, to get the Order Detail From Paypal
Array
(
    [USER] => PAYPAL_API_EMAIL
    [PWD] => PAYPAL_API_PASSWORD
    [SIGNATURE] => PAYPAL_API_SIGNATURE
    [VERSION] => 95
    [url] => https://api-3t.sandbox.paypal.com/nvp
    [METHOD] => GetExpressCheckoutDetails
    [TOKEN] => EC-06N33746DC1035058
)

After calling above API, response will be return

Step 6: Charge Money From Customer We need to a new paypal method to charge the customer i.e DoExpressCheckoutPayment
Request Params Example
Array
(
    [USER] => PAYPAL_API_EMAIL
    [PWD] => PAYPAL_API_PASSWORD
    [SIGNATURE] => PAYPAL_API_SIGNATURE
    [VERSION] => 95
    [url] => https://api-3t.sandbox.paypal.com/nvp
    [METHOD] => DoExpressCheckoutPayment
    [TOKEN] => EC-06N33746DC1035058
    [PAYERID] => HS5JSKPGX4T2G
    [L_PAYMENTREQUEST_0_NAME0] => Om Belt - Black
    [L_PAYMENTREQUEST_0_DESC0] => Obscure Belts
    [L_PAYMENTREQUEST_0_AMT0] => 82.92
    [L_PAYMENTREQUEST_0_QTY0] => 1
    [PAYMENTREQUEST_0_CURRENCYCODE] => USD
    [PAYMENTREQUEST_0_SHIPPINGAMT] => 5.15
    [PAYMENTREQUEST_0_AMT] => 88.07
    [PAYMENTREQUEST_0_ITEMAMT] => 82.92
    [PAYMENTREQUEST_0_SELLERPAYPALACCOUNTID] => receiver_email_address//payment will receive by this paypal email
   
)

When we call DoExpressCheckoutPayment, We will charge the payment from customer Account and an email will be sent to customer and receiver.

Step 7: Check Resonse
Array
(
    [TOKEN] => EC-06N33746DC1035058
    [SUCCESSPAGEREDIRECTREQUESTED] => false
    [TIMESTAMP] => 2014-08-11T12:41:32Z
    [CORRELATIONID] => e5d0b3a5e674a
    [ACK] => Success
    [VERSION] => 95
    [BUILD] => 12301660
    [INSURANCEOPTIONSELECTED] => false
    [SHIPPINGOPTIONISDEFAULT] => false
    [PAYMENTINFO_0_TRANSACTIONID] => 6HD429843V086060C
    [PAYMENTINFO_0_TRANSACTIONTYPE] => cart
    [PAYMENTINFO_0_PAYMENTTYPE] => instant
    [PAYMENTINFO_0_ORDERTIME] => 2014-08-11T12:41:32Z
    [PAYMENTINFO_0_AMT] => 88.07
    [PAYMENTINFO_0_FEEAMT] => 2.85
    [PAYMENTINFO_0_TAXAMT] => 0.00
    [PAYMENTINFO_0_CURRENCYCODE] => USD
    [PAYMENTINFO_0_PAYMENTSTATUS] => Completed
    [PAYMENTINFO_0_PENDINGREASON] => None
    [PAYMENTINFO_0_REASONCODE] => None
    [PAYMENTINFO_0_PROTECTIONELIGIBILITY] => Eligible
    [PAYMENTINFO_0_PROTECTIONELIGIBILITYTYPE] => ItemNotReceivedEligible,UnauthorizedPaymentEligible
    [PAYMENTINFO_0_SELLERPAYPALACCOUNTID] => receiver_email_address
    [PAYMENTINFO_0_SECUREMERCHANTACCOUNTID] => UFBX24KTUHJKU
    [PAYMENTINFO_0_ERRORCODE] => 0
    [PAYMENTINFO_0_ACK] => Success
)


Customer can pay with Credit card without Paypal Account

  1. Login to Paypal
  2. Go to Profile subtab
  3. Click on Website Payment Preferences under the Selling Preferences column
  4. Check the yes box under PayPal Account Optional