1

I've a many documents like this

user:62e13ae4f9a38f7610e70bd7,
_id :62e13ae4f9a38f7610e70bdb
transactions:{

 {
            "amount": 50,
            "category": "Bills",
            "type": "Expense",
            "date": "2022-01-20T00:00:00.000Z",
            "_id": "62e13ae4f9a38f7610e70be0"
        },
        {
            "amount": 100,
            "category": "Lottery",
            "type": "Income",
            "date": "2022-01-20T00:00:00.000Z",
            "_id": "62e13ae4f9a38f7610e70be1"
        },
        {
            "amount": 200,
            "category": "Salary",
            "type": "Income",
            "date": "2022-01-20T00:00:00.000Z",
            "_id": "62e13ae4f9a38f7610e70be2"
        }
}


And I want to retrieve an object from the transactions array with a particular id (I only want that object )

I tried some methods like

      const transactions = await Transactions.find({
        user: req.user._id,
        "transactions._id": {$eq: req.params._id },
      });

  const transactions = await Transactions.find({
        user: req.user._id,
        "transactions": { _id: req.params._id },
      });
            const transactions = await Transactions.find({
        user: req.user._id,
        "transactions": { $elemMatch:{_id: req.params._id }},
      });

but nothing seems to be working, can anyone help me to solve this And please mention the mistake I made.

2 Answers 2

2

Try to match the transactions._id directly:

"transactions._id": req.params._id

Example

const transactions = await Transactions.find({
  user: req.user._id,
  "transactions._id": req.params._id
});

Update

From the comment, it's possible to use projection as the second parameter of .find() to return only the object it found in the transactions.

const transactions = await Transactions.find({
  user: req.user._id,
  "transactions._id": req.params._id
}, { "transactions.$": 1 });

More information

Sign up to request clarification or add additional context in comments.

2 Comments

thanks BTW:) after some modification I got the answer now const transactions = await Transactions.find({ user: req.user._id, "transactions._id":req.params.id , }, { "transactions.$":1 });
Oh, I missed that part, the answer is updated with your solution.
1

If you only want that matching element, you have to use aggreation.

db.collection.aggregate([
{
  $unwind: "$transactions"
},
{
  $match: {
    "transactions.id": "idGoesHere"
  }
}
])

As you commented in the other answer, you could use positional operator to project the matching elements as well.

3 Comments

can tell me what does the $unwind do here
Unwind is an aggregation operator which temporarily reshapes the doc. Means one array element will be one separate doc.
Thanks for the help,but actually i don't need the aggregation here .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.