コード例 #1
0
ファイル: basic-test.js プロジェクト: node4good/grist
 coll.find({num: 10}, {'sub.num': 1}).toArray(function (err, docs) {
     if (err) throw err;
     var doc = docs[0];
     assert.equal(_.size(doc), 2);
     assert.equal(doc.sub.num, 10);
     done();
 });
コード例 #2
0
ファイル: Cursor.js プロジェクト: node4good/grist
Cursor.prototype.sort = function (v, cb) {
    if (_.isNumber(cb) || _.isString(cb)) { // handle sort(a,1)
        v = {v: cb};
        cb = null;
    }

    if (this._i) this._err = new Error('Cursor is closed');

    if (this._err) return this;

    if (!_.isObject(v)) {
        if (!_.isString(v)) {
            this._err = new Error("Illegal sort clause, must be of the form [['field1', '(ascending|descending)'], ['field2', '(ascending|descending)']]");
        } else {
            this._sort = v;
            this._order = 1;
        }
    } else {
        if (_.size(v) <= 2) {
            if (_.isArray(v)) {
                if (_.isArray(v[0])) {
                    this._sort = v[0][0];
                    this._order = v[0][1];
                } else {
                    this._sort = v[0];
                    this._order = 1;
                }
            } else {
                this._sort = _.keys(v)[0];
                this._order = v[this._sort];
            }
            if (this._sort) {
                if (this._order == 'asc')
                    this._order = 1;
                if (this._order == 'desc')
                    this._order = -1;
                if (!(this._order == 1 || this._order == -1))
                    this._err = new Error("Illegal sort clause, must be of the form [['field1', '(ascending|descending)'], ['field2', '(ascending|descending)']]");
            }
        } else this._err = new Error("Multi field sort is not supported");
    }

    var self = this;
    if (cb)
        process.nextTick(function () {cb(self._err, self);});

    return this;
};
コード例 #3
0
ファイル: basic-test.js プロジェクト: node4good/grist
 coll.find({num: 10}, {'num': 1}).toArray(safe.sure(done, function (docs) {
     assert.equal(_.size(docs[0]), 2);
     assert.equal(docs[0].num, 10);
     done();
 }));