Skip to content

Instantly share code, notes, and snippets.

@meowoodie
Last active August 29, 2015 14:24
Show Gist options
  • Save meowoodie/31adc21d804311c7a098 to your computer and use it in GitHub Desktop.
Save meowoodie/31adc21d804311c7a098 to your computer and use it in GitHub Desktop.
LeanCloud上“一句话”query所有内容

Query all data on LeanEngine

Python

  • Function ahead of codes of dao in Python
def _find(query):
    result = []
    count  = query.count()
    pages  = count/1000 + 1
    print count
    for i in range(pages):
        _query = copy.deepcopy(query)
        _query.limit(1000)
        _query.skip(i * 1000)
        res = _query.find()
        for item in res:
            result.append(item)
    return result
  • Demo
from leancloud import Query, Object

UserSenz = Object.extend("UserSenz")
User     = Object.extend("User")

query = Query(UserSenz)
user  = User()
user.id = user_id
query.equal_to("user", user)
query.greater_than("timestamp", start_time)
query.less_than("timestamp", end_time)
query.ascending("timestamp")
behavior = _find(query)

NodeJS

  • Function ahead of codes of dao in NodeJS
var _findAll = function (query){
    return query.count().then(
        function (count){
            var promises = [];
            var pages    = Math.ceil(count/1000);
            for (var i = 0; i <= pages; i ++){
                var _query = _.clone(query);
                _query.limit(1000);
                _query.skip(i*1000);
                promises.push(_query.find());
            }
            return AV.Promise.all(promises);
        },
        function (error){
            return AV.Promise.error(error);
        }
    ).then(
        function (results){
            var rebuid_result = [];
            results.forEach(function (result_list){
                result_list.forEach(function (list_item){
                    rebuid_result.push(list_item);
                });
            });
            return AV.Promise.as(rebuid_result);
        },
        function (error){
            return AV.Promise.error(error);
        }
    );
};
  • Demo
query.ascending("timestamp");
query.limit(1000);
_findAll(query).then(
    function (result) {
        return AV.Promise.as(result);
    },
    function (error) {
        return AV.Promise.error(error);
    }
);

tips

The function is used for querying all data. So there is no need to add addtion limit condition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment