简体   繁体   中英

Pagination with sub field in mongodb

I want to query from mongodb sub field and can pagination for it. Thankss!

  {
    "_id": "616d274c655e0000ee005f32",
    "subscription": [
      {
        "account_admin": "5fe5707399cc690013a159b2"
      },
      {
        "account_admin": "61b9bac72258040012e218d5"
      }
    ]
  },
  {
    "_id": "616fe5786b1b0000a9007a95",
    "subscription": [
      {
        "account_admin": "5fe5707399cc690013a159b2"
      },
      {
        "account_admin": "61b9bac72258040012e218d5"
      },
      {
        "account_admin": "5df739c4dfb7940013388e5f"
      },
      {
        "account_admin": "61b9b21daaa08000115f4da3"
      }
    ],
  }
]

How to find mongodb by field account_admin: "61b9bac72258040012e218d5" to data below (pagination if can)

====>

 [ 
  {
   "account_admin": "61b9bac72258040012e218d5"
  },
  {
   "account_admin": "61b9bac72258040012e218d5"
  }
 ];

Getting sub-fields are can be done with ' dot notation '. Pagination is simply using query.limit() and query.skip() .

I've not tested the code but this should work.

const query = Model.find({ "subscription.account_admin": "61b9bac72258040012e218d5" });

query.limit(documentsPerPage);
query.skip(documentsPerPage * currentPageNumber);

const results = await query.exec();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM