- MongoDb is schemaless. Yani diyelim bir obje için tanımladıgımız 3 özellik var.Diğeri içinde 3 özellik olmak zorunda değil.Diğeri için 4 özellik olabilir.Ya da demin tanımladığımız 3 özelliği olan objeyi 4 özellikli olrak değişitirebiliriz.
>use test
>db.users.insert({'name':'Gamze Sen','city_of_birth':'Izmir'})
>db.users.find().pretty()
{
"_id" : ObjectId("557d63066d2ca190ccde6bf1"),
"name" : "Gamze Sen",
"city_of_birth" : "Izmir"
}
Bir eleman ekleyelim.Onun da 3 özelliği olsun.
>db.users.insert({'name':'Tugce','city_of_birth':'Izmir','favorite_color':'pink'})
>db.users.find().pretty()
{
"_id" : ObjectId("557d63066d2ca190ccde6bf1"),
"name" : "Gamze Sen",
"city_of_birth" : "Izmir"
}
{
"_id" : ObjectId("557d63756d2ca190ccde6bf2"),
"name" : "Tugce",
"city_of_birth" : "Izmir",
"favorite_color" : "pink"
}
Şimdi istersen 'name':'Gamze' olan elemana bir eleman daha ekleyerek onu da 3 elemanlı yapabiliriz.
>var j = db.users.findOne('name':'Gamze')
>j.favorite_color:'Blue'
>db.users.save(j)
>db.users.find().pretty()
{
"_id" : ObjectId("557d63756d2ca190ccde6bf2"),
"name" : "Tugce Sen",
"city_Of_birth" : "Izmir",
"favorite_color" : "pink"
}
{
"_id" : ObjectId("557d63066d2ca190ccde6bf1"),
"name" : "Gamze Sen",
"city_of_birth" : "Izmir",
"favorite_color" : "Blue"
}
JSON DATA STRUCTURES
There are 2 types of data structures in JSON.They are arrays and dictionaries.Arrays are list of things.Dictionaries are associative maps.
Arrays are written in [......]
Dictionaries are written in {key:value,......}
Documents are starts with dictionaries.
{"name":"value", "city":"value", interests: [.......this is array.these are dictionaries.written in {}]}
JSON SUBDOCUMENT
Example : Write a JSON document with a single key, "address" that has as it value another document with the keys “street_address”, “city”, “state”, “zipcode”, with the following values: “street_address” is "23 Elm Drive", “city” is "Palo Alto", “state” is "California", “zipcode” is "94305".
{"address":{"street_address":"23 Elm Drive","city":"Palo Alto","state":"California","zipcode":"94305"}}
Conditional Statements in Python
LIST:
>>fruit=['apple','orange','banana']
>>if 'apple' in fruit:
... print("there is an apple")
>>
>>there is an apple
DICTIONARIES IN PYTHON
Like in Json. But there is no order in Python Dictionaries.
>>g={"name":"gamze","surname":"sen"}
>>g
{'surname': 'sen', 'name': 'gamze'}
>>g['surname']="abc"
>>g
{'surname': 'abc', 'name': 'gamze'}
>> g.keys()
{'surname', 'name'}
>>'surname' in g
True
>>'last_name' in g
False
*If we check the item in the list, it only looks whether or not key.
>>'gamze' in g
False
Example:
Initialize a new dict named "colors" with the following key values pairs: sky is blue, sea is blue. earth is brown. Note: Please preserve the order of these keys when you enter your answer.
>>colors = {'sky': 'blue', 'sea' : 'blue', 'earth': 'brown'}
EXCEPTIONS IN PYTHON
Exception hata demektir.Yazdığımız kodda hatalar çıktığı zaman bunu bilmek isteriz.Bu yüzden kodumuzda try exception bloğunu kullanırız.Bunun içinse kodumuzun başına import sys yazmamız gerekir.
exception.py adıda örnek bir program yazalım:
import sys
try:
print 5/0
except Exception as e:
print "exception: ", type(e) , e
print "but life goes on"
Çalıştırdığımızda şu outputu elde ederiz:
>>exception: <type 'exceptions.ZeroDivisionError'> integer division or modulo by zero
but life goes on
Hiç yorum yok:
Yorum Gönder