#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import json
from pymongo import MongoClient
import datetime
#获取系统当前日期
today = datetime.datetime.now()
#取指定前几天的日期
lastday = today + datetime.timedelta(days=-4)
#连接mongod的方式采用url
client = MongoClient(‘mongodb://user:[email protected]:27017/KYELog‘)
#指定mongdb的数据库
db = client[‘KYELog‘]
#指定数据库的集合名称
collection = db[‘AutoLogisticsPHLog‘]
#mongo的日期格式new Date,python调用时不能判断,需要转化为datetime时间
#for i in collection.find({"LogDT":{‘$lt‘:new Date("2018-02-09T01:16:33.303Z")}}):
#查询指定日期之前的数据find({"LogDT":{‘$lt‘:datetime.datetime(2018,2,8,12,55,59)}})
#for i in collection.find({"LogDT":{‘$lt‘:datetime.datetime(lastday.year,lastday.month,lastday.day,7,53,11)}}):
# print (i)
#删除指定日期之前的数据collection.remove({"LogDT":{‘$lt‘:datetime.datetime(2018,2,8)}})
collection.remove({"LogDT":{‘$lt‘:datetime.datetime(lastday.year,lastday.month,lastday.day,5,58,58)}})
原文地址:https://www.cnblogs.com/liqing1009/p/8438653.html