【问题标题】:Mongoose, returns whole document, not just part of itMongoose,返回整个文档,而不仅仅是它的一部分
【发布时间】:2012-02-13 23:53:44
【问题描述】:

我希望来自 mongodb 的 mongoose 查询回调结果仅包含文档的某个部分。现在,以下代码返回整个文档,而不仅仅是假定的切片数组部分。任何线索为什么?在数据库中,pending 实际上包含超过 10 个元素。谢谢

var NotificationsReference = new Schema({

    id             : Number, //fbid
    unRead         : Number,
    pendingSize    : Number,
    pending        : [Notification]

});

NotificationsReference.find({ id: userId}, { pending: { $slice: [skip, 5]}}, function(err, result){
                   if(err || result === null){
                        callback("Failed");
                   }
                   else{
                       callback(result);
                   }
                });

【问题讨论】:

    标签: javascript node.js mongodb mongoose


    【解决方案1】:

    尝试为此使用 Mongoose API:

    NotificationsReference
        .findById(userId)
        .where('pending')
        .slice([skip, 5])
        .run(function(err, docs){
            console.log(err ? err : docs);
        })
    

    【讨论】:

    猜你喜欢
    • 2023-04-11
    • 2018-06-08
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 2018-10-01
    相关资源
    最近更新 更多