代码之家  ›  专栏  ›  技术社区  ›  hasherBaba

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

  •  0
  • hasherBaba  · 技术社区  · 6 年前

    我打算按顺序一次取100个身份证。 我 find ID大于 skip 其中skip可以在开始时设置为默认值。我需要对 find() 限制是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 回复  |  直到 6 年前
        1
  •  1
  •   Sergio Tulentsev    6 年前

    如果您想要默认的“最小”值,那么空对象id更好。它是同一类型(objectid),排序最低。

    ObjectId('000000000000000000000000')
    

    或者,可以在执行查询时进行分支。是第一个问题吗?如果是,则不包括跳过部分。如果没有,请使用以前结果中的最后一个ID。