当前位置: 首页>>代码示例>>Python>>正文


Python Database.dereference方法代码示例

本文整理汇总了Python中pymongo.database.Database.dereference方法的典型用法代码示例。如果您正苦于以下问题:Python Database.dereference方法的具体用法?Python Database.dereference怎么用?Python Database.dereference使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pymongo.database.Database的用法示例。


在下文中一共展示了Database.dereference方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: dbref_test

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import dereference [as 别名]
    def dbref_test(self):
        db = Database(self._get_connection(), "pymongo_test")

        try:
            db.create_collection('owners')
            db.create_collection('tasks')
            db.create_collection('tasks_ref')

            # owners and tasks
            db.owners.insert({"name":"Jim"})
            db.tasks.insert([
                {"name": "read"},
                {"name": "sleep"}
                ])

            # update jim with tasks: reading and sleeping
            reading_task = db.tasks.find_one({"name": "read"})
            sleeping_task = db.tasks.find_one({"name": "sleep"})

            jim_update = db.owners.find_one({"name": "Jim"})
            jim_update["tasks"] = [
                DBRef(collection = "tasks", id = reading_task["_id"]),
                DBRef(collection = "tasks", id = sleeping_task["_id"])
                ]

            db.owners.save(jim_update)

            # get jim fresh again and display his tasks
            fresh_jim = db.owners.find_one({"name":"Jim"})
            print "tasks are:"
            for task in fresh_jim["tasks"]:
                print db.dereference(task)["name"]

            db.tasks_ref.insert( { "ref" :  DBRef(collection = "tasks", id = reading_task["_id"]) })
            db.tasks_ref.insert( { "ref" :  DBRef(collection = "tasks", id = sleeping_task["_id"]) })
            r1 = db.tasks_ref.find( { "ref" : DBRef(collection = "tasks", id = reading_task["_id"]) })
            print r1.count()
        finally:
            db.drop_collection('owners')
            db.drop_collection('tasks')
            db.drop_collection('tasks_ref')
开发者ID:appneta,项目名称:python-traceview,代码行数:43,代码来源:test_pymongo.py

示例2: resolve_object_ids

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import dereference [as 别名]
    def resolve_object_ids(self, mongo_object):
        if mongo_object is None:
            return None

        if type(mongo_object) is "dict":
            result = { }
            for key in mongo_object:
                result[key] = self.resolve_object_ids(mongo_object[key]) if key is not "_id" else mongo_object[key]
            return result
        elif type(mongo_object) is "list":
            result = []
            for item in mongo_object:
                result.append(self.resolve_object_ids(item))
            return result
        elif type(mongo_object) is "bson.objectid.ObjectId":
            return self.get_single(mongo_object)
        elif type(mongo_object) is "bson.dbref.DBRef":
            return Database.dereference(mongo_object)
开发者ID:SkyLapse,项目名称:DMS,代码行数:20,代码来源:basemodel.py

示例3: encode_model

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import dereference [as 别名]
def encode_model(obj, recursive=False):
    if obj is None:
        return obj
    if isinstance(obj, (Document, EmbeddedDocument)):
        out = dict(obj._data)
        for k, v in out.items():
            if isinstance(v, ObjectId):
                if k is None:
                    del(out[k])
                else:
                    # Unlikely that we'll hit this since ObjectId is always NULL key
                    out[k] = str(v)
            else:
                out[k] = encode_model(v)
    elif isinstance(obj, ModuleType):
        out = None
    elif isinstance(obj, groupby):
        out = [(gb, list(l)) for gb, l in obj]
    elif isinstance(obj, (list)):
        out = [encode_model(item) for item in obj]

    elif isinstance(obj, (dict)):
        out = dict([(k, encode_model(v)) for (k, v) in obj.items()])

    elif hasattr(obj, 'isoformat'):
        out = obj.isoformat()
    elif isinstance(obj, ObjectId):
        return None
    elif isinstance(obj, (str, unicode, int)):
        out = obj
    elif isinstance(obj, float):
        out = str(obj)
    elif isinstance(obj, DBRef):
        db = Database(g.db, 'listenitlater')
        obj_deref = Database.dereference(db, obj)
        out = encode_model(obj_deref)
    else:
        raise TypeError, "Could not JSON-encode type '%s': %s" % (type(obj), str(obj))
    return out
开发者ID:gianpaj,项目名称:crately,代码行数:41,代码来源:db.py


注:本文中的pymongo.database.Database.dereference方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。