0

I have made several efforts to select a single specific document that contains the minimum value from the database.

       let Lowestdate = await BTCMongo.aggregate(
        [ 

          // { "$sort": { "name": 1, 
          {
            $match : { "createdAt"  : { $gte: new Date(last),$lte: new Date(NEW) } } },
          
             {

            $group:
            {
              _id:null,
              minFee_doc:{$min: "$$ROOT"},
              minFee: { $min:{$toDouble:"$one"}},
              firstFee: { $first: "$one" },
              lastFee: { $last: "$one" },
              maxFee: { $max: {$toDouble:"$one"}},
              
            }
          },
        ]
       ).then(result => {}):

with minFee_doc:{$min: "$$ROOT"}, I have been trying to return the document containing the minimum $min but it keeps returning document containing $first

How do i select the document with minimum value?

Note : i will like to return the whole document including the "CreatedAt" "UpdatedAt", and _id. of the document containing the minimum value

Expected Result should look like:

{
        "minFee_doc": {
            "_id": "61e84c9f622642463640e05c",
            "createdAt": "2022-01-19T17:38:39.034Z",
            "updatedAt": "2022-04-24T14:48:38.100Z",
            "__v": 0,
            "one": 2
        },
        "minFee": 2,
        "firstFee": 3,
        "lastFee": 5,
        "maxFee": 6
    }

Edit: also to provide a single document not multiple

  • 1
    It would be better if you can provide testing data and expected result in a valid JSON by editing your own question. Ask a question like [this](https://stackoverflow.com/questions/71835902/excract-only-the-lookups-return-mongoose) would be more clarify for us to solve your problem. Make sure that your testing data can come up with your expected result in certain logic. – YuTing Apr 27 '22 at 00:35
  • The question has been edited @YuTing – Moses Adeyinka Apr 27 '22 at 01:16

1 Answers1

0

$push all docs in $group then $set the array with $filter

db.collection.aggregate([
  {
    $match: {}
  },
  {
    $group: {
      _id: null,
      minFee_doc: { $push: "$$ROOT" },
      minFee: { $min: { $toDouble: "$one" } },
      firstFee: { $first: "$one" },
      lastFee: { $last: "$one" },
      maxFee: { $max: { $toDouble: "$one" } }
    }
  },
  {
    $set: {
      minFee_doc: {
        $filter: {
          input: "$minFee_doc",
          as: "m",
          cond: { "$eq": [ "$$m.one", "$minFee" ] }
        }
      }
    }
  }
])

mongoplayground

YuTing
  • 6,555
  • 2
  • 6
  • 16
  • Hi Thanks, for the answer but i will like to return the whole document including the "CreatedAt" "UpdatedAt", and _id. of the document containing the minimum value – Moses Adeyinka Apr 27 '22 at 01:09
  • @MosesAdeyinka my answer actually return the whole document including the "CreatedAt" "UpdatedAt", and _id you can add as many field as you want in my mongoplayground left side and run it to see result – YuTing Apr 27 '22 at 01:12
  • @MosesAdeyinka check out my mongoplayground below my answer. It is actually the same as you want – YuTing Apr 27 '22 at 01:18
  • @MosesAdeyinka is this answer what you need ? – YuTing Apr 27 '22 at 01:23
  • yes a little more help pls the result returns multiple results and i tried to use `{ $limit : 1}` and .limit(1) but doesn't work :) – Moses Adeyinka Apr 27 '22 at 01:28
  • 1
    like I comment in your question. It would be better if you can provide **testing data** and **expected result** in a **valid JSON** by editing your own question. Ask a question like this would be more clarify for us to solve your problem. **Make sure that your testing data can come up with your expected result in certain logic**. It seems you want more than your question. – YuTing Apr 27 '22 at 01:37
  • Thanks it edited but that was unexpected because i thought `$limit` can handle it – Moses Adeyinka Apr 27 '22 at 01:55