账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    在node.js中使用原生mogodb驱动限制数组返回数量,同样语法在命令行中起作用,在node中不生效
    28
    0

    在node.js中使用原生mogodb驱动限制数组返回数量,同样语法在命令行中起作用,在node中不生效

    我想做一个分页查询的DEMO,想使用db.collection.find({"_id":ObjectId(_id)},{"comments":{"$slice":1}})可是查询出来的结果仍是把数组comments的数据全部查询出来了

    代码如下
         dbase
            .collection("userInfo")
            .findOne({ "_id": ObjectId(user_id)},{"comments":{"$slice":1}}, function(err, item) {
              if (err) {
                reject("faith");
                throw new Error("查询出错");
              } else {
                let articalData = [];
                for (let i = 0; i < item.comments.length; i++) {
                  articalData.push({
                    _id: item.comments[i].id,
                    desc: item.comments[i].desc,
                    content: item.comments[i].content,
                    authorName: item.comments[i].name,
                    title: item.comments[i].title,
                    time: item.comments[i].time
                  });
                }
                let data = {
                  code: 0,
                  name: item.name,
                  articalData: articalData,
                  sumPage:item.comments.length%2?Number.parseInt(item.comments.length/2)+1:item.comments.length/2
                };
                resolve([dbase, data]);
              }
            });

    没有报错,返回的是全部数据,期望看到根据限制条件返回的数据。

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部