Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

python - pymongo: name 'ISODate' is not defined

I have problem when i try to select data in mongodb with pymongo, this is my code :

import pymongo
from pymongo import MongoClient
import sys
from datetime import datetime

try:
    conn=pymongo.MongoClient('10.33.109.228',27017)
    db=conn.mnemosyne
    data_ip=db.session.aggregate({'$match':{'timestamp':{'$gte': ISODate('2016-11-11T00:00:00.000Z'),'$lte': ISODate('2016-11-11T23:59:59.000Z')}}},{'$group':{'_id':'$source_ip'}})
    for f in data_ip:
        print f['_id']

except pymongo.errors.ConnectionFailure, e:
    print "Could not connect to MongoDB: %s" % e

and when i execute it i have some error like this:

Traceback (most recent call last):
  File "test.py", line 9, in <module>
    data_ip=db.session.aggregate({'$match':{'timestamp':{'$gte': ISODate('2016-11-11T00:00:00.000Z'),'$lte': ISODate('2016-11-11T23:59:59.000Z')}}},{'$group':{'_id':'$source_ip'}})
NameError: name 'ISODate' is not defined

I want the result like this:

{ "_id" : "60.18.133.207" }
{ "_id" : "178.254.52.96" }
{ "_id" : "42.229.218.192" }
{ "_id" : "92.82.171.117" }
{ "_id" : "103.208.120.205" }
{ "_id" : "185.153.208.142" }

this is example structure of mydatabase:

> db.session.findOne()

    {
            "_id" : ObjectId("5786398d1f50070f31f27f7c"),
            "protocol" : "epmapper",
            "hpfeed_id" : ObjectId("5786398d1f50070f31f27f7b"),
            "timestamp" : ISODate("2016-07-13T12:52:29.112Z"),
            "source_ip" : "23.251.55.182",
            "source_port" : 2713,
            "destination_port" : 135,
            "identifier" : "d3374f14-48f7-11e6-9e19-0050569163b4",
            "honeypot" : "dionaea"
    }

Please help me to fix the error

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

ISODate is a function in the Mongo shell, which is a javascript environment, it's not available within Python.

You can use dateutil for converting a string to datetime object in Python,

import dateutil.parser
dateStr = "2016-11-11T00:00:00.000Z"
dateutil.parser.parse(dateStr)  # returns a datetime.datetime(2016, 11, 11, 00, 0, tzinfo=tzutc())

Using PyMongo, if you want to insert datetime in MongoDB you can simply do the following:

import pymongo
import dateutil
dateStr = '2016-11-11T00:00:00.000Z'
myDatetime = dateutil.parser.parse(dateStr)
client = pymongo.MongoClient()
client.db.collection.insert({'date': myDatetime})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...