#url : https://docs.mongodb.com/manual/reference/sql-comparison/
#url : http://www.runoob.com/python3/python-mongodb.html
import pymongo
myclient = pymongo.MongoClient('mongodb://192.168.168.237:27017/',username='mongoadmin',password='mongoadmin')
dblist = myclient.list_database_names()
# dblist = myclient.database_names()
#if "scrapy_db" in dblist:
# print("數據庫已存在!")
db=myclient['scrapy_db']
mytable = db['books']
#x= mytable.find_one()
#print(x)
'''
#===========新增資料=====
#單筆
data = {"name":"飛翔的魔女","quote":"1"}
db.wishList.insert_one (data)
#多筆
data = [
{"name":"妖怪公寓的優雅日常","quote":"1"},
{"name":"月刊少女野崎同學","quote":"1"}
]
db.wishList.insert_many(data)
'''
'''
#=====更新資料=====
query = {"name":{"$regex":"飛翔"}}
newvalue = {"$set":{"quote":"23985213"}}
x = db.wishList.find_one(query)
print("原始資料:"+x)
y=db.wishList.update_many(query,newvalue)
print(y.modified_count,"資料已修改")
'''
'''
#=========查詢==========
#SQL沒下條件
print("1.")
for x in mytable.find():
print(x)
print("2.")
for x in db.books.find():
print(x)
SQL指定條件
for a in db.books.find({"title":"月刊少女野崎同學(1)"}):
print(a)
# SQL的Like,正規表示法
print(3.)
for Y in db.books.find( { "title" :{ '$regex': "月刊少女" } } ) :
print(Y)
#distinct
for y in mytable.distinct("title"):
print(y)
'''
[Python]Python使用Mongo的語法
跟常用的SQL不太一樣,建議直接看第一個url,上面有SQL跟mongo的語法比對
0 意見:
張貼留言