解決pymongo裡操作IOSDate型別的問題

2022-06-22 07:15:13 字數 1404 閱讀 3780

pymongo是python對mongodb的操作庫。但是由於python沒有iosdate型別,所以對mongo的時間型別是個很麻煩的操作。整理乙個把python能識別的date型別轉化為iosdate型別

指令碼如下:

#!/usr/bin/python

import

pymongo

from dateutil import

parser

datestr = '

2018-06-12t00:00:00.000z

'mydatetime =parser.parse(datestr)

client = pymongo.mongoclient(host="

192.168.2.14

",port=29019)

db =client.dbs

db.authenticate()

db.ceshi.insert()

到mongodb裡檢視資料:

testin.dbs>db.ceshi.find()

已經插入isodate格式時間。

import

pymongo,datetime

from dateutil import

parser

deffindmongo():

time1 =datetime.datetime.now()

datestr1 = '

2019-01-21t00:00:00z

'datestr2 = '

2019-01-23t00:00:00z

'mydatetime1 =parser.parse(datestr1)

mydatetime2 =parser.parse(datestr2)

client = pymongo.mongoclient(host="

0.0.0.0

", port=3717)

db =client.mongolilly

db.authenticate(

"root

","admin")

a = db.current_temp.find(})

time2 =datetime.datetime.now()

time3 = time2 -time1

print

"time3------->select_time :%s

" %time3

list =

for i in

a : data =

time4 =datetime.datetime.now()

time5 = time4-time1

print

"time5------>all_time:

",time5

pymongo 操作集錦

匯入模組 import pymongo 建立mongoclient連線,需先啟動已經安裝的mongodb服務 client mongoclient localhost 27017 或者 client mongoclient mongodb localhost 27017 進入資料庫 db clien...

pymongo基礎 常用操作

res await ss classroom.update one newrecord 插入處理 try oldrecord await ss attendencelog.find one filter duprecodfilter,sort created time 1 delres await ...

pymongo 增刪改查基本操作彙總

mongo conn mongo user a.查詢所有 mongo conn.find b.單條記錄查詢 mongo conn.find one c.多條記錄查詢 對於多條資料的查詢,我們可以使用find 方法,例如在這裡查詢年齡為20的資料,示例如下 results mongo conn.fin...