mongodbpythonPython已看

python MongoDB数据库操作与连接和ORM

2021-12-27  本文已影响0人  Cache_wood

@[toc]

数据库连接与操作

关系数据库:PostgreSQL、MySQL
非关系数据库:MongoDB

pymongo使用
from pymongo import MongoClient
client = MongoClient()
client = MongoClient('localhost:27017/')
db = client.test_database
db = client['test-database']
collection = db.test_collection
collection = db['test-collection']

查找所有数据库的名字

import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
dblist = myclient.list_database_names()
for db in dblist:
    print(db)
mydb = myclient["student"]
mycol = mydb["comments"]
myclient.close()
admin
config 
local  
student
insert_one()
insert_many()
from pymongo import MongoClient
import os
import sys
texts=[]
with open('w15\mongo_texts.txt',encoding='utf-8') as f:
    for line in f:
        items=line.strip().split('\t')
        texts.append({"text":items[0],"score":int(items[1])})
print('load %d lines' % len(texts))
client=MongoClient('localhost',27017)
db=client.student
collection=db.comments
result=collection.insert_many(texts)
print(result)
client.close()
load 20000 lines
<pymongo.results.InsertManyResult object at 0x000001CF7FB8FF08>

returns a single document matching a query (or None if there are no matches)

To get more than a single document as the result of a query

import sys
from pymongo import MongoClient

#0 差评;1 好评;

with MongoClient('localhost',27017) as client:
    db=client.student
    collection=db.comments

    re=collection.find_one()
    re=collection.find_one({"score":1})
    if 'text' in re:
        print(re['text'])
    res=collection.find({"score":0}).limit(5)  #int(sys.argv[1])}).limit(5)
    for index, doc in enumerate(res,start=1):
        if 'text' in doc and 'score' in doc:
            print(index,doc['score'],doc['text'])
开机有点烦锁,联网时有些软件会自动下载安装!!
1 0 用着用着就卡死了 卖家只会说是正常的 太坑了
2 0 垃圾电脑,慢死,刚买的就卡。跟三年前老电脑一个德行,
3 0 服务太垃圾了,客服不理人,根本就没有保障,电脑卡的要死,多开几个网面就死机不动。我怀疑是不是翻新机。这样的店铺怎么不早点 
关闭。妈的
4 0 破电脑 我已经去修了两次了 今天是第三次 我想知道 动不动黑屏 开不了机是什么鬼!!!我已经无力吐槽了非常的火大    我想知道上
个 qq  看个电视都卡的卡的电脑   是怎么做打游戏不卡的?
5 0 体验感不好,一是没有发票提供,二是装不了一些系统,如GIS,这都没法工作的,郁闷不是一点点
from pymongo import MongoClient
with MongoClient('localhost',27017) as client:
    db=client.student
    collection=db.comments

    c=collection.count_documents({"text":{"$regex":"机箱"}})
    print(c)
    docs=collection.find({"text":{"$regex":"机箱"}})
    for doc in docs:
        print(doc['text'])
1
买之前纠结了很久,本来想买个迷你的台式机箱自己组装个电脑,但是看到台式机箱后面那一坨线心理真是不舒服,还是笔记本好,干干净净
,携带方便,用于工作和娱乐都没有问题,外出不好携带的!电脑外观很漂亮,重量也很轻,关键是自带512G的固态硬盘,速度杠杠的,亲们 
!别犹豫了买吧!
from pymongo import MongoClient
with MongoClient('localhost',27017) as client:
    db=client.student
    collection=db.comments

    docs=collection.find({"text":{"$regex":"开机"}}).sort("score",1)
    for doc in docs:
        print(doc['text'], doc['score'])
delete_one(myquery)
delete_many(myquery)
delete_many({}) #删除collection中的所有文档
drop()  #删除整个collection
myquery = { "address": "Valley 345" }
newvalues = { "$set": { "address": "Canyon 123" } }
update_one(myquery, newvalues) • myquery = { "address": { "$regex": "^S"}}
newvalues = { "$set": { "name": "Minnie" }}
x = mycol.update_many(myquery, newvalues)

ORM简介

上一篇下一篇

猜你喜欢

热点阅读