具有单个文档字符串数组字段的服务器端分页 [英] Server side pagination with single document string array field

查看:31
本文介绍了具有单个文档字符串数组字段的服务器端分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何对包含 blacklistGroup: [String] 字段的单个文档进行服务器端分页

I'm trying to figure out how to do server side pagination for a single document that contains a blacklistGroup: [String] field

  "blacklistGroup": [
    "5e99fd3aa506cf570056898d",
    "5e99fde5a506cf5700568991",
    "5e99fd64a506cf570056898e",
    "5e98c950f4fb3f63b4634e30",
    "5e99fd15a506cf570056898c",
    "5e99fda5a506cf570056898f",
    "5e99fdc7a506cf5700568990",
    "5e99fcf3a506cf570056898b",
    "5e98e90d85e69146f841d23a",
    "5e9867ff5e72550988820dd3",
    "5e98e8e785e69146f841d239"
  ]

我希望它一次限制为 10 个.我基本上在寻找相当于

I want it to limit 10 at a time. I'm basically looking for the equivalent to

     User.findById({ _id: req.params.id })
              .select("blacklistGroup")
              .populate("blacklistGroup", "username")
              .skip(10 * (req.query.currentPage - 1))
              .limit(10)
              .then(documents => {

但显然不是针对多个文档,而是针对被查询的单个文档.我目前正在将当前页面传递到后端以检索该特定页面的 10 列表.此外,返回用户名也很重要,因此在示例中为 .populate.感谢您对此的任何帮助!

but obviously not for multiple documents, but for the single document being queried. I'm currently passing the current page to the backend to retrieve that specific page's list of 10. Also it's important to return the username hence the .populate in example. I appreciate any help with this!

推荐答案

您可以在查询中使用切片,然后使用填充.

You could use slice on query followed by populate.

User.findById({ _id: req.params.id })
           .slice('blacklistGroup', [10 * (req.query.currentPage - 1), 10])
           .populate("blacklistGroup", "username");

您还可以将聚合与 $slice 和 $lookup 结合使用.

You could also use aggregate with $slice and $lookup.

User.aggregate([
 {"$match":{"_id":req.params.id}},
 {"$project":{"blacklistGroup":{"$slice":["$blacklistGroup", 10 * (req.query.currentPage - 1), 10]}}},
 {"$lookup":{
   "from": usercollection,
   "localField": "blacklistGroup",
   "foreignField": "_id",
   "as": "blacklistGroup"
 }},
 {"$project":{"usernames":"$blacklistGroup.username"}}])

您可以使用更新的查找版本进一步增强它.

You could furthur enhance it using newer lookup version.

User.aggregate([
 {"$match":{"_id":req.params.id}},
 {"$lookup":{
  "from":usercollection,
  "let":{"blacklistGroup":{"$slice":["$blacklistGroup", 10 * (req.query.currentPage - 1), 10]}},
  "pipeline":[
    {"$match":{"$expr":{"$eq":["$_id","$$blacklistGroup"]}}},
    {"$project":{"username":1,"_id":0}}
  ],
  "as":"blacklistGroup"
}}]);

为了完整起见添加了整个示例

For the sake of completeness added the whole example

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test', {useNewUrlParser: true});
var db = mongoose.connection;
mongoose.set('debug', true);
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  // we're connected!
});
const UserSchema = new mongoose.Schema({
    blacklistGroup: [{
      type: String,
      ref: 'Group'
    }]
  })
const GroupSchema = new mongoose.Schema({
    _id: String,
    content: String,
    username: String
  })
const User  = mongoose.model('User', UserSchema, 'users');
const Group = mongoose.model('Group', GroupSchema, 'groups');
module.exports = {
    User, Group
  }

 User.findOne().slice("blacklistGroup", 3).populate("blacklistGroup", "username").exec((err, blacklistGroup) => {
      console.log("Populated blacklistGroup " + blacklistGroups);
    })        

还添加了 Mongo Playground likn 示例 2

Also added Mongo Playground likn for example 2

https://mongoplayground.net/p/YP5l5Kz3sp8

这篇关于具有单个文档字符串数组字段的服务器端分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆