Python action MONGO script

Source: Internet
Author: User
Tags mongoclient

#!/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 Call cannot be judged, it needs to be converted to datetime time
#for I in Collection.find ({"Logdt": {' $lt ': New Date ("2018-02-09t01:16:33.303z")}}):

#查询指定日期之前的数据find ({"Logdt": {' $lt ':d atetime.datetime (2018,2,8,12,55,59)}})
#for I in Collection.find ({"Logdt": {' $lt ':d atetime.datetime (lastday.year,lastday.month,lastday.day,7,53,11)}}):
# Print (i)

#删除指定日期之前的数据collection. Remove ({"Logdt": {' $lt ':d atetime.datetime (2018,2,8)}})
Collection.remove ({"Logdt": {' $lt ':d atetime.datetime (lastday.year,lastday.month,lastday.day,5,58,58)}})

Python action MONGO script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.