How to use the pymongo.DESCENDING function in pymongo

To help you get started, we’ve selected a few pymongo examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mdirolf / simple-messaging-service / View on Github external
def GET(self, page=None):
        if page is None:
            page = 0
        page = int(page)

        previous = None
        if page > 0:
            previous = page - 1

        next = None
        if db.messages.count() > (page + 1) * page_size:
            next = page + 1

        return render.index(db.messages.find().sort("date", DESCENDING).limit(page_size).skip(page * page_size),
                            previous, next)
github picoCTF / picoCTF / picoCTF-web / api / View on Github external
def get_api_exceptions(result_limit=50):
    Retrieve the most recent logged exceptions.

        result_limit: the maximum number of exceptions to return.

        list of exception dicts

    db = api.db.get_conn()
    results = (
        db.exceptions.find({"visible": True}, {"_id": 0})
        .sort([("time", pymongo.DESCENDING)])
    return list(results)
github rexbu / MeCloud / server / helper / View on Github external
def find(self, collection, query, keys=None, sort=None, limit=8, skip=0):
        results = []
        if sort == None:
            items = self.db[collection].find(MongoDb.toBson(query), keys).sort('_id', pymongo.DESCENDING).skip(skip).limit(limit)
            items = self.db[collection].find(MongoDb.toBson(query), keys).sort(self.sortToTuple(sort)).skip(0).limit(limit)
        for item in items:
        return results
github gunthercox / ChatterBot / chatterbot / storage / View on Github external
def get_latest_response(self, conversation_id):
        Returns the latest response in a conversation if it exists.
        Returns None if a matching conversation cannot be found.
        from pymongo import DESCENDING

        statements = list(self.statements.find({
            '': conversation_id
        }).sort('conversations.created_at', DESCENDING))

        if not statements:
            return None

        return self.mongo_to_object(statements[-2])
github MongoEngine / mongoengine / mongoengine / queryset / View on Github external
>>> qs._get_order_by(['-last_name', 'first_name'])
        [('last_name', -1), ('first_name', 1)]
        key_list = []
        for key in keys:
            if not key:

            if key == "$text_score":
                key_list.append(("_text_score", {"$meta": "textScore"}))

            direction = pymongo.ASCENDING
            if key[0] == "-":
                direction = pymongo.DESCENDING

            if key[0] in ("-", "+"):
                key = key[1:]

            key = key.replace("__", ".")
                key = self._document._translate_field_name(key)
            except Exception:
                # TODO this exception should be more specific

            key_list.append((key, direction))

        return key_list
github cbyn / bitpredict / app / View on Github external
actual = (data.future_price/data.current_price).apply(log)
returns = actual*data.position*100*100

p1 = figure(title='Phony Returns',
            x_axis_label='Greenwich Time',
            y_axis_label='Basis Points')
p1.line(times, returns.cumsum(), name='all_returns')

renderer1 ='all_returns'))
ds1 = renderer1[0].data_source

while True:
    cursor = predictions.find().sort('_id', pymongo.DESCENDING)
    data = pd.DataFrame(list(cursor))
    data = data[data.future_price != 0]
    data = data.set_index('_id')
    data = data.sort_index(ascending=True)
    times = pd.to_datetime(data.index, unit='s').to_series()
    actual = (data.future_price/data.current_price).apply(log)
    returns = actual*data.position*100*100['x'] = times['y'] = returns.cumsum()

github Surendhar95 / ChatBot / chatbot / chatbot / chatterbot / storage / View on Github external
query = query.statement_response_list_contains(
            del kwargs['in_response_to__contains']

        query = query.raw(kwargs)

        matches = self.statements.find(query.value())

        if order_by:

            direction = pymongo.ASCENDING

            # Sort so that newer datetimes appear first
            if order_by == 'created_at':
                direction = pymongo.DESCENDING

            matches = matches.sort(order_by, direction)

        results = []

        for match in list(matches):

        return results
github pdroalves / encrypted-mongodb / src / secmongo / View on Github external
from .index.indexnode import IndexNode
from bson.json_util import dumps
from multiprocessing import Queue
from bson import ObjectId
import json
import time
import os
from itertools import chain,islice

class StopLookingForThings(Exception):

class SecMongo:
    RANGE_OP = 42
    GREATER_OP = 1
    LOWER_OP = -1

    client = None
    db = None
    collection = None

    __ciphers = {"references": None, "h_add": None, "h_mul": None}

    def __init__(self, add_cipher_param=None, url=None):
        assert url is None or type(url) == str

        # Connects to database
        if url:
github yougov / mongo-connector / mongo_connector / View on Github external
# Of these documents, which is the most recent?
        last_inserted_doc = max(last_docs,
                                key=lambda x: x["_ts"] if x else float("-inf"))

        # Nothing has been replicated. No need to rollback target systems
        if last_inserted_doc is None:
            return None

        # Find the oplog entry that touched the most recent document.
        # We'll use this to figure where to pick up the oplog later.
        target_ts = util.long_to_bson_ts(last_inserted_doc['_ts'])
        last_oplog_entry = util.retry_until_ok(
            {'ts': {'$lte': target_ts}, 'op': {'$ne': 'n'}},
            sort=[('$natural', pymongo.DESCENDING)]

        LOG.debug("OplogThread: last oplog entry is %s"
                  % str(last_oplog_entry))

        # The oplog entry for the most recent document doesn't exist anymore.
        # If we've fallen behind in the oplog, this will be caught later
        if last_oplog_entry is None:
            return None

        # rollback_cutoff_ts happened *before* the rollback
        rollback_cutoff_ts = last_oplog_entry['ts']
        start_ts = util.bson_ts_to_long(rollback_cutoff_ts)
        # timestamp of the most recent document on any target system
        end_ts = last_inserted_doc['_ts']
github materialsproject / MPWorks / mpworks / snl_utils / View on Github external
def build_groups(self, mpsnl, force_new=False, snlgroup_guess=None, testing_mode=False):
        # testing mode is used to see if something already exists in DB w/o adding it to the db

        match_found = False
        if not force_new:
            if snlgroup_guess:
                sgp = self.snlgroups.find_one({'snlgroup_id': snlgroup_guess})
                snlgroup = SNLGroup.from_dict(sgp)
                match_found, spec_group = self._add_if_belongs(snlgroup, mpsnl, testing_mode)

            if not match_found:
                # look at all potential matches
                for entry in self.snlgroups.find({'snlgroup_key': mpsnl.snlgroup_key},
                                                 sort=[("num_snl", DESCENDING)]):
                    snlgroup = SNLGroup.from_dict(entry)
                    match_found, spec_group = self._add_if_belongs(snlgroup, mpsnl, testing_mode)
                    if match_found:

        if not match_found:
            # add a new SNLGroup
            snlgroup_id = self._get_next_snlgroup_id()
            snlgroup = SNLGroup(snlgroup_id, mpsnl)
            if snlgroup.species_groups:
                spec_group = snlgroup.species_groups.keys()[0]
            if not testing_mode:

        return snlgroup, not match_found, spec_group