I have a class:
import sys
import os
import pymongo
from pymongo import MongoClient
class Collection():
def __init__(self, db, collection_name):
self.db = db
self.collection_name = collection_name
if not hasattr(self.__class__, 'client'):
self.__class__.client = MongoClient()
self.data_base = getattr(self.client, self.db)
self.collection = getattr(self.data_base, self.collection_name)
I created class instances as follows:
def getCollections(self):
collections_dict = {}
for i in range(len(self.db_collection_names)):
collections_dict[self.db_collection_names[i]] = Collection(self.database_name, self.db_collection_names[i])
return collections_dict
db_collection_names contains email_logs. I created an emails instance as follows:
emails = collections_dict['email_logs']
print emails yields <collection.Collection instance at 0x105ce6248>
print emails.find() yields:
Traceback (most recent call last):
File "main.py", line 75, in <module>
program.runProgram()
File "main.py", line 63, in runProgram
print emails.find
AttributeError: Collection instance has no attribute 'find'
Why the error?
Not sure why you're bothering to redefine the Pymongo Collection class.
from pymongo import MongoClient
db = MongoClient().my_database # create an instance of pymongo Database
emails = db.emails # create an instance of pymongo Collection
emails.find() # yeilds a pymongo Cursor (containing the query results)
I was missing the collection before the find() method
emails.collection.find()
Related
I have a database SensorReadings which has a collection MeterReadings this is populated using another script so the collection already exists.
I am setting up a script to query the MeterReadings collection. It looks like the following.
## Imports ##
import pymongo
## Variables ##
url = "mongodb://localhost:27017"
## getDBConnection ##
def getDBConnection():
client = pymongo.MongoClient(url)
db = client["SesnorReadings"]
collection = db["MeterReadings"]
readings = collection.find_one({})
for res in readings:
print(res)
readings is returning a None type. The exact error I get is shown below.
Traceback (most recent call last):
File "main.py", line 6, in <module>
conn = functions.getDBConnection()
File "functions.py", line 19, in getDBConnection
for res in readings:
TypeError: 'NoneType' object is not iterable
When I add an insert statement hwoever first and then query using the find_one method, it will create a new database SensorReadings with a collection MeterReadings in it.
## Imports ##
import pymongo
## Variables ##
url = "mongodb://localhost:27017"
## getDBConnection ##
def getDBConnection():
client = pymongo.MongoClient(url)
db = client["SesnorReadings"]
collection = db["MeterReadings"]
readings = collection.find_one({})
mydict = { "name": "John", "address": "Highway 37" }
x = collection.insert_one(mydict)
for res in readings:
print(res)
The above code returns:
_id
name
address
{'_id': ObjectId('5fc679dd439f1a27d0bb0c5e'), 'name': 'John', 'address': 'Highway 37'}
However in the Mongo terminal there are now two SensorReading databases. One has the correct data and one has the data I just added using the above snippet.
> show dbs
SensorReadings 0.000GB
SesnorReadings 0.000GB
admin 0.000GB
config 0.000GB
local 0.000GB
I just want to connect to the SesnorReadings database that is already created and query the MeterReadings collection in it.
find_one returns one document. Use find to get all documents.
And you have a typo in collection name as pointed out in the comments.
I am pretty new to Python and trying to use a very simple one table DB.
I'm using python 3, peewee and pymysql. The database is MySQL, local on my Windows 10 PC (wampserver) and the code is;
import pymysql
pymysql.install_as_MySQLdb()
import MySQLdb
import peewee
from peewee import *
db = MySQLdb.connect(user='xxxxx', passwd='xxxxx',
host='localhost', port=3306, db='xxxxx')
class SearchUrl(peewee.Model):
id = peewee.AutoField()
url = peewee.TextField()
page_title = peewee.TextField()
date_found = peewee.DateField()
date_last_changed = peewee.DateField()
article_status = peewee.CharField()
description = peewee.TextField()
class Meta:
database = db
newUrl = SearchUrl.create(url='http://cropnosis.co.uk', page_title="Cropnosis Ltd.",
date_found='2018-04-13', status='new', description='Cropnosis website')
newUrl.save()
for url in SearchUrl.filter(url='http://cropnosis.co.uk'):
print(url.description)
db.close()
Whenever I run this I get the following error in the "SearchUrl.create" line. I have searched for the 'returning_clause' attribute but have found little or no mention of it esp. with regard to MySQL.
Any help or relevant links greatly appreciated.
Traceback (most recent call last):
File "researcherDb.py", line 26, in <module>
date_found='2018-04-13', status='new', description='Cropnosis website')
File "C:\.dev\Client\Cropnosis\Researcher\lib\site-packages\peewee.py", line 5161, in create
inst.save(force_insert=True)
File "C:\.dev\Client\Cropnosis\Researcher\lib\site-packages\peewee.py", line 5284, in save
pk_from_cursor = self.insert(**field_dict).execute()
File "C:\.dev\Client\Cropnosis\Researcher\lib\site-packages\peewee.py", line 5128, in insert
return ModelInsert(cls, cls._normalize_data(__data, insert))
File "C:\.dev\Client\Cropnosis\Researcher\lib\site-packages\peewee.py", line 5868, in __init__
if self.model._meta.database.returning_clause:
AttributeError: 'Connection' object has no attribute 'returning_clause'
You want this:
from peewee import *
# this is peewee.MySQLDatabase, not pymysql's connection class.
db = MySQLDatabase('the_db_name', user='xxxxx', passwd='xxxxx')
I am new to SQLAlchemy and jumped into it for its ease of use and event mechanisms. I am looking to execute a method after some data is inserted but I keep running into several errors of the type "Attribute[...]" related to the object I am referring to
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, ForeignKey
from sqlalchemy import Integer, UnicodeText
from sqlalchemy import event
#variables feching is hidden
db = create_engine("mysql://"+dbLogin+":"+dbPassword+"#"+dbAddress+"/"+dbDatabase)
metadata = MetaData(db)
#the log table is not explicitly defined but rather uses the autoload function
logtable = Table('logs', metadata, autoload=True)
#and here I am trying to start "processLogChanged" after_insert on the "logs" table
#event.listens_for(logtable, "after_insert")
def processLogChanged():
print "---- the log table has changed"
I am obviously doing something wrong but I have been scouring through a lot of questions/answers and can't find the right here, it would be great if you could give me a hand
Here is the stack for instance in that case
Traceback (most recent call last):
File "optra.py", line 144, in <module>#event.listens_for(logtable, "after_insert")
File "/home/jpp/anaconda2/lib/python2.7/site-packages/sqlalchemy/event/api.py", line 124, in decorate
listen(target, identifier, fn, *args, **kw)
File "/home/jpp/anaconda2/lib/python2.7/site-packages/sqlalchemy/event/api.py", line 89, in listen
_event_key(target, identifier, fn).listen(*args, **kw)
File "/home/jpp/anaconda2/lib/python2.7/site-packages/sqlalchemy/event/registry.py", line 194, in listen
dispatch_collection = getattr(target.dispatch, identifier)
File "/home/jpp/anaconda2/lib/python2.7/site-packages/sqlalchemy/event/base.py", line 95, in __getattr__
raise AttributeError(name)
AttributeError: after_insert
Thank you
after_insert event assumes you are using tables declared by sqlalchemy.ext.declarative.declarative_base. And you need to specify mapper, connection, target arguments for event handler:
Base = declarative_base(metadata)
class LogTable(Base):
__tablename__ = 'logs'
id = Column(Integer)
# Some other columns
#event.listens_for(LogTable, 'after_insert')
def processLogChanged(mapper, connection, target):
print "---- the log table has changed"
When trying to connect to MongoDB , I'm experiencing an error
How can I solve this?
Traceback (most recent call last):
File "D:/MongoDB-U/Python/Codes/Try.py", line 17, in
print (item['name'])
TypeError: 'NoneType' object has no attribute 'getitem'
Code:
import pymongo
from pymongo import MongoClient
connection = MongoClient('localhost',27017)
db = connection.test
names = db.things
item = things.find_one()
print (item['name'])
You're creating a names collection variable but then using a things collection variable in your find_one call. It should be:
db = connection.test
things = db.things
item = things.find_one()
print (item['name'])
I have a mapped class in ming
from ming import Session, create_datastore
from ming import schema
from ming.odm import ODMSession
from ming.odm.mapper import MapperExtension
from ming.odm.property import ForeignIdProperty
from ming.odm.property import FieldProperty, RelationProperty
from ming.odm.declarative import MappedClass
import config
bind = create_datastore(config.DATABASE_NAME)
session = Session(bind)
odm_session = ODMSession(doc_session=session)
class Document(MappedClass):
class __mongometa__:
session = odm_session
name = 'document'
_id = FieldProperty(schema.ObjectId)
Now, I want to do a simple query to it as
Document.query.get(_id="51e46f782b5f9411144f0efe")
But it doesn't work. Documentation is not quite clear about it. I know that in mongodb shell we have to wrap the id in an ObjectId object, but I can't get it to work in Python
You should try the query with ObjectId
from bson.objectid import ObjectId
Document.query.get(_id=ObjectId('51e46f782b5f9411144f0efe'))
With naked pymongo
from bson.objectid import ObjectId
from pymongo import Connection
connection = Connection()
db = connection['lenin']
collection = db.document
collection.find_one({'_id': '51e35ee82e3817732b7bf3c1'}) # returns None
collection.find_one({'_id': ObjectId('51e35ee82e3817732b7bf3c1')}) # returns the object