简体   繁体   中英

How can I return an array of bids made by the current user

I have a collection of adverts, inside that there is a array of objects of bids made by other users to that specific advert.
The thing I want to return is only the bids made by the current user so it has to match with req.user.id (where i hold the jwt token)
So I tried filter method but it returned an empty array, then tried with for in loop, got another empty set of square brackets...


Here is my 'adverts' collection;
For example, from the collection below, i want to get an array of objects only contains bids
made by this user (user : 5f7c40a0ec1a374c6c924610).
 [ { "_id": "5f7d924371469c21f866fa16", "user": "5f7c40a0ec1a374c6c924610", "title": "Logistics for carrying chemicals", "text": "We need a approximately 20 trucks of logistics line for carrying our chemicals", "status": false, "company": "Bayrak Company", "location": "San Francisco", "date": "2020-10-07T10:02:43.811Z", "bids": [ { "_id": "5f7dc3225a835c359432ab15", "user": "5f7c40a0ec1a374c6c924610", "bid": "50000", "company": "Bayrak Company", "avatar": "https://gravatar.com/avatar/4ecfd3d216693adc6867bf09c9087b1a?d=mm&r=pg&s=200", "date": "2020-10-07T13:31:14.890Z" }, { "_id": "5f7db5fea762d34054072f8c", "user": "5f7c40a0ec1a374c6c924610", "bid": "50000", "company": "Bayrak Company", "avatar": "https://gravatar.com/avatar/4ecfd3d216693adc6867bf09c9087b1a?d=mm&r=pg&s=200", "date": "2020-10-07T12:35:10.237Z" } ], "comments": [], "__v": 2 }, { "_id": "5f7db0153870fd4178390a4d", "user": "5f7c40a0ec1a374c6c924610", "title": "Need construction trucks for new buildings", "text": "The new constrution area needs approximately 15 trucks", "status": false, "company": "Bayrak Company", "location": "San Francisco", "date": "2020-10-07T12:09:57.362Z", "bids": [], "comments": [], "__v": 0 } ]


**And here is what I tried so far; **

 // @route GET /api/adverts/mybids // @desc Get bids that I made // @access Private. router.get('/mybids', auth, async (req, res) => { try { let adverts = await Advert.find(); let mybids = []; let bidsByMe = []; for (let x in adverts) { let bid = adverts[x].bids; for (let y in bid) { let innerBid = bid[y]; mybids.push(innerBid); } } for (let x in mybids) { let bid = mybids[x]; if (bid.user == req.user.id) { bidsByMe.push(bid); } } res.json(bidsByMe); } catch (err) { console.error(err.message); res.status(500).send('Server Error...'); } });

You can run this query :

  db.collection.aggregate([
 {
   $match: {
  user: "5f7c40a0ec1a374c6c924610",
  "bids.user": "5f7c40a0ec1a374c6c924610"
 }
},
{
$group: {
  _id: "$_id",
  user: {$first: "$user"},
  title: {$first: "$title"},
  text: {$first: "$text"},
  status: {$first: "$status"},
  location: {$first: "$location"},
  bids:{$first:"$bids"}
  
  }
}
 ])

//check below URL 

https://mongoplayground.net/p/TBGPDXO5lIl

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