OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

I am unable to get Mongoose Aggregate method to work

  • Thread starter Thread starter valve
  • Start date Start date
V

valve

Guest
I am building an api and I wanted to use the skip and limit methods in mongoose to handle pagination. I would like to get the total document size along with the pagination data. Initially, I made two queries: one for the paginated data and the other for total document size. This is bad, right? I started searching if there is an inbuilt way to handle this on a single query. I came across aggregate method. Unfortunately, I have not been able to get it to work.

Here are my codes with two queries:

Code:
const page = req.query.page
 const {skip, searchlimit} = getPagination(params);
let transactionsData = []

const transactions = await TransactionModel.find({transactionOwnerId: user?._id}).sort({createdAt:-1})
  .skip(skip)
  .limit(searchlimit);

 totalDocumentSize = await TransactionModel.countDocuments({transactionOwnerId:   user?._id});

  transactionsData = transactions as transactionAttributes[];

console.log(transactionsData, totalDocumentSize)

Here, I tried the aggregate method:

Code:
 const me = await TransactionModel.aggregate([{
           $match: {transactionOwnerId: user?._id}
        }, 
         {
            $sort: {createdAt:-1}
         }
        
     ]);
console.log(me);

I believe that I am not applying the aggredate method properly. It is returning an empty array. How can I use it to apply the skip and limit methods and also get the total document size?

<p>I am building an api and I wanted to use the skip and limit methods in mongoose to handle pagination.
I would like to get the total document size along with the pagination data. Initially, I made two queries: one for the paginated data and the other for total document size. This is bad, right? I started searching if there is an inbuilt way to handle this on a single query. I came across aggregate method. Unfortunately, I have not been able to get it to work.</p>
<p>Here are my codes with two queries:</p>
<pre><code>const page = req.query.page
const {skip, searchlimit} = getPagination(params);
let transactionsData = []

const transactions = await TransactionModel.find({transactionOwnerId: user?._id}).sort({createdAt:-1})
.skip(skip)
.limit(searchlimit);

totalDocumentSize = await TransactionModel.countDocuments({transactionOwnerId: user?._id});

transactionsData = transactions as transactionAttributes[];

console.log(transactionsData, totalDocumentSize)
</code></pre>
<p>Here, I tried the aggregate method:</p>
<pre><code> const me = await TransactionModel.aggregate([{
$match: {transactionOwnerId: user?._id}
},
{
$sort: {createdAt:-1}
}

]);
console.log(me);
</code></pre>
<p>I believe that I am not applying the aggredate method properly. It is returning an empty array. How can I use it to apply the skip and limit methods and also get the total document size?</p>
 

Latest posts

M
Replies
0
Views
1
MusicLovingIndianGirl
M
Q
Replies
0
Views
1
quora question
Q
Top