如何在mongoDB中使用$limit和$sort与Q.nbind()一起使用

How to use $limit and $sort with Q.nbind() in mongoDB

本文关键字:nbind 一起 sort limit mongoDB      更新时间:2023-09-26

我在我的应用程序中使用了Q模块。我想使用 $limit 和 $sort 函数来查询 mongoDB,我也在使用 Q.nbind() 进行查询

var Q = require('q');
var mongoose = require('mongoose');
var RequestPrayers = mongoose.model('requestPrayers');
var RPrayerfind = Q.nbind(RequestPrayers.find, RequestPrayers);

function getSingleRParyerInfo(FrndID){
    var id = FrndID
    var prayerInfo={prayersInfo:''};
    return RPrayerfind({userID:id, is_notPrivate:true})
        //   return   find()
//  ^^^^^^ Rule 1
        .then(function(Prayers) {
//  ^^^^^ Rule 3
            if (!Prayers){
                prayerInfo.prayersInfo = '';
                //   console.log(User)
            }else{
                prayerInfo.prayersInfo = Prayers;
                //console.log(User)
            }
            return prayerInfo;
//      ^^^^^^ Rule 3b
        });
}

请问我应该用什么来获得我想要的结果

返回 RPrayerfind({userID:id, is_notPrivate:true}).limit(2);

return RPrayerfind({userID:id, is_notPrivate:true},{$limit:2})

这很简单,只需查看以下代码:

    var q = require('q');
    var findSomeThing = q.nbind(SomeThing.find,SomeThing);
    SomeThing({"someId":"someIdValue"},{},{limit:2}).done(function(data){
        //2 records will be retrieved here
        console.log(data);

    });

希望对您有所帮助!

你可以像这样简单地使用它

RPrayerFind({userId:1},{},{'$limit':10,'$skip':300})