我应该使用什么默认ID,以便我的集合中的所有ID都大于此ID?

时间:2018-06-18 14:03:42

标签: mongodb pymongo

我打算以排序的方式一次获取100个ID。 我find大于skip的ID,其中skip可以在开头设置为默认值。我需要对find()中生成的ID进行排序,限制集为100。 所以,我的查询是:

db['Organization'].find({"_id":{"$gt":ObjectId(skip)}},{"_id":1}).sort([("_id",1)]).limit(100)

截至目前,我已将skip设置为str(0)。我打算用迭代中提取的最后一个id来更新它。

完整的终点是:

@hug.get('/organization/collect_pricing')
def get_organizations():
    start_time = datetime.strptime('2016-11-01', '%Y-%m-%d')
    org_ids = []
    org_pricing_plans = []
    counter = 0
    skip = str(0)
    result_check = True
    pricing_response = []
    ob_toolbox = Toolbox()
    while(result_check is True):
        print(counter)
        try:
            if 
            organizations = db['Organization'].find({"_id":{"$gt":ObjectId(skip)}},{"_id":1}).sort([("_id",1)]).limit(100)
        except Exception as e:
            print(e)
        if organizations.count(True) == 0:
            result_check = False
            continue
        counter += 100
        for org in organizations:
            org_ids.append("Organization$"+org["_id"])
        try:
            pricing_plans = ob_toolbox.bulk_fetch(collection="PricingPlan", identifier="_p_organization", ids=org_ids)
        except Exception as e:
            print(e)
        currDict = {}
        for i in range(0, organizations.count(True)):
            currDict["id"] = org_ids[i]
            currDict["expiresAt"] = pricing_plans[i]["expiresAt"]
            currDict["resources"] = pricing_plans[i]["resources"]
            currDict["_created_at"] = pricing_plans[i]["_created_at"]
            org_pricing_plans.append(currDict)
            print(currDict["id"])
            skip = currDict["id"]
        if organizations.count(True) < 100:
            result_check = False
    return (org_pricing_plans)

1 个答案:

答案 0 :(得分:1)

如果你想要默认的&#34; minimal&#34; value,则null对象id更好。它的类型相同(ObjectId)并且排序最低。

ObjectId('000000000000000000000000')

或者,您可以在进行查询时进行分支。这是第一次查询吗?如果是,请不要包含跳过部分。如果不是,请使用之前结果中的最后一个ID。