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 / sms.py 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 / logger.py View on Github external
def get_api_exceptions(result_limit=50):
    """
    Retrieve the most recent logged exceptions.

    Args:
        result_limit: the maximum number of exceptions to return.

    Returns:
        list of exception dicts

    """
    db = api.db.get_conn()
    results = (
        db.exceptions.find({"visible": True}, {"_id": 0})
        .sort([("time", pymongo.DESCENDING)])
        .limit(result_limit)
    )
    return list(results)
github rexbu / MeCloud / server / helper / DbHelper.py 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)
        else:
            items = self.db[collection].find(MongoDb.toBson(query), keys).sort(self.sortToTuple(sort)).skip(0).limit(limit)
        for item in items:
            results.append(MongoDb.toJson(item))
        return results
github gunthercox / ChatterBot / chatterbot / storage / mongodb.py 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({
            'conversations.id': conversation_id
        }).sort('conversations.created_at', DESCENDING))

        if not statements:
            return None

        return self.mongo_to_object(statements[-2])
github MongoEngine / mongoengine / mongoengine / queryset / base.py 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:
                continue

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

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

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

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

            key_list.append((key, direction))

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

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

renderer1 = p1.select(dict(name='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

    ds1.data['x'] = times
    ds1.data['y'] = returns.cumsum()
    cursession().store_objects(ds1)

    time.sleep(1)
github Surendhar95 / ChatBot / chatbot / chatbot / chatterbot / storage / mongodb.py View on Github external
query = query.statement_response_list_contains(
                kwargs['in_response_to__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):
            results.append(self.mongo_to_object(match))

        return results
github pdroalves / encrypted-mongodb / src / secmongo / secmongo.py 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):
    pass


class SecMongo:
    ASCENDING = pymongo.ASCENDING
    DESCENDING = pymongo.DESCENDING
    RANGE_OP = 42
    EQUALITY_OP = 0
    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 / oplog_manager.py 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(
            self.oplog.find_one,
            {'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 / snl_mongo.py 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:
                        break

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

        return snlgroup, not match_found, spec_group