Issue with multiple records of the same subdocument returned for MongoDb and Python -
i'm getting strange issue when getting results of object subdocuments mongo database using python , pymongo.
i have document list of sub documents, e.g.
user: { "_id": .... hats: [{"colour": "blue" }] }
i query using find_one(). returns details of document , 1 sub document record. however, next query 2 "hats", second being duplicate of first. next 3 "hats" , in continues on that.
if restart application, "count" above reset, find_one() query returns 1 sub document again.
there 1 sub document record in database, isn't issue. must doing weird in terms of in memory stuff.
i using python "tornado" framework, application tornado.wsgi.wsgiapplication. every time new request comes should opening new connection.
the request handler along lines of
class handler(requesthandler): def initialize(self): self.db = mongoclient("localhost", 27017)
i'm baffled be.
ultimately nothing mongodb.
for each collection type in database had model class. decouple exact details of how data stored how returned server.
class user(modelbase): name = none hats = [] @staticmethod def from_db(document): model = user() model.name = document.get("name") hats = document.get("hats", list()) document in hats: hat = hat.from_db(document) model.hats.append(hat) return model
the problem defined properties on model directly instead of setting in init function.
that is
class user(modelbase): name = none hats = []
instead of
class user(modelbase): def __init__(self): self.name = none self.hats = []
i thought equivalent, seems when variables set on class directly static variables shared between instances of class. when added hat list 1 user object, next user object had hat in list , added one.
Comments
Post a Comment