Skip to main content

Posts

Kotlin If Expression

Kotlin if Expression In Kotlin, if is an expression is which returns a value. It is used for control the flow of program structure. There is various type of if expression in Kotlin. if-else expression if-else if-else ladder expression nested if expression Traditional if Statement Syntax of traditional if statement if(condation){      //code statement } Syntax of traditional if else statement if(condation){      //code statement } else{      //code statement } Kotlin if-else Expression As if is an expression it is not used as standalone, it is used with if-else expression and the result of an if-else expression is assign into a variable.   Syntax of if-else expression val returnValue = if (condation) {      //code statement } else {      // code statement }      println(returnValue) Kotlin if-else Expression Example Let's see an example of if-else expression. fun main(args: Array ) {      val num1 = 10

Kotlin Operator

Kotlin Operator Operators are special characters which perform operation on operands (values or variable).There are various kind of operators available in Kotlin. Arithmetic operator Relation operator Assignment operator Unary operator Bitwise operation Logical operator Arithmetic Operator Arithmetic operators are used to perform basic mathematical operations such as addition (+), subtraction (-), multiplication (*), division (/) etc.   + Addition a+b a.plus(b) - Subtraction a-b a.minus(b) * Multiply a*b a.times(b) / Division a/b a.div(b) % Modulus a%b a.rem(b) Example of Arithmetic Operator fun main(args : Array ) {      var a=10;      var b=5;      println(a+b);      println(a-b);      println(a*b);      println(a/b);     println(a%b); } Relation Operator Relation operator shows the relation and compares between operands. Following are the different relational operators: > greater than a>b a.compateTo(b)>0 < Less than a >= greate

PYTHON MONGODB LIMIT

M ONGODB LIMIT Limit the Result To limit the result in MongoDB, we use the limit() method. The limit() method takes one parameter, a number defining how many documents to return. Limit the result to only return 5 documents: import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"] myresult = mycol.find().limit(5) for x in myresult:   print(x) {'_id': ObjectId('6325d5001d9e9d4c027643b8'), 'name': 'Kartik'} {'_id': ObjectId('6325e1ef7e4337dbd6d8b9a1'), 'name': 'Kartik'} {'_id': ObjectId('6325e20d0bea1e2841fcdd06'), 'name': 'Kartik'} {'_id': ObjectId('6325e2381039de66af625c07'), 'name': 'Kartik'} {'_id': ObjectId('6325e26da4ef8259d33e1a33'), 'name': 'Kartik'}

PYTHON UPDATE COLLECTION

U PDATE COLLECTION Update Collection You can update a record, or document as it is called in MongoDB, by using the update_one() method. The first parameter of the update_one() method is a query object defining which document to update. Note: If the query finds more than one record, only the first occurrence is updated. The second parameter is an object defining the new values of the document. Change the address from "Valley" to "Canyon": import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"] myquery = { "address": "Valley 345" } newvalues = { "$set": { "address": "Canyon 123" } } mycol.update_one(myquery, newvalues) #print "customers" after the update: for x in mycol.find():   print(x) Update Many To update all documents that meets the criteria of the query, use the update_

PYTHON MONGODB DROP COLLECTION

M ONGODB DROP COLLECTION Delete Collection You can delete a table, or collection as it is called in MongoDB, by using the drop() method. Delete the "customers" collection: import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"] mycol.drop() The drop() method returns true if the collection was dropped successfully, and false if the collection does not exist.

PYTHON MONGODB DELETE

M ONGODB DELETE Delete Document To delete one document, we use the delete_one() method. The first parameter of the delete_one() method is a query object defining which document to delete. Note: If the query finds more than one document, only the first occurrence is deleted. Delete the document with the address "Mountain 21": import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"] myquery = { "address": "Mountain 21" } mycol.delete_one(myquery) Delete Many Documents To delete more than one document, use the delete_many() method. The first parameter of the delete_many() method is a query object defining which documents to delete. Delete all documents were the address starts with the letter S: import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol =

PYTHON MONGODB SORT

M ONGODB SORT Sort the Result Use the sort() method to sort the result in ascending or descending order. The sort() method takes one parameter for "fieldname" and one parameter for "direction" (ascending is the default direction). Sort the result alphabetically by name: import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"] mydoc = mycol.find().sort("name") for x in mydoc:   print(x) Sort Descending Use the value -1 as the second parameter to sort descending. sort("name", 1) #ascending sort("name", -1) #descending Sort the result reverse alphabetically by name: import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"] mydoc = mycol.find().sort("name", -1) for x in mydoc:   print(x)