Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
How to retreive an object from an array of Objects in mongodb given a objectid

I have an array of reviews, I want to retrieve only a review from an array of objects inside a schema.

Here is my schema:

const sellerSchema = new mongoose.Schema({
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User",
    unique: true,
  },
  reviews: [
    {
      by: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "User",
        unique: true,
      },
      title: {
        type: String,
      },
      message: {
        type: String,
      },
      rating: Number,
      imagesUri: [{ String }],
      timestamp: {
        type: Date,
        default: Date.now,
      },
    },
  ],
});

How can I get a single review from the array if 'by' will be req.user._id. I have the previous code, but it is not working to retrieve the review only that satisfies the query.

try {
    const seller_id = mongoose.Types.ObjectId(req.params._id);
    const review = await Seller.findOne(
      { _id: seller_id },
      { reviews: { $elemMatch: { by: req.user._id } } } //get the review that matches to the user_id
    );
    res.status(200).send(review);
  } catch (err) {
    //sends the status code with error code message to the user
    res.status(502).send({
      error: "Error retreiving review.",
    });
  }

This retrieves the whole seller document, but I just want to retrieve the object review with the given a user_id === by: ObjectID

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

give it a try

try {
const seller_id = mongoose.Types.ObjectId(req.params._id);
const review = await Seller.findOne(
  { _id: seller_id , "reviews.by": req.user._id}, { "reviews.$": 1 }
);
res.status(200).send(review);

}

about 4 years ago · Juan Pablo Isaza Report

0

Try to cast also the user _id as ObjectId and unify your conditions into a single object:

try {
  const seller_id = mongoose.Types.ObjectId(req.params._id);
  const user_id = mongoose.Types.ObjectId(req.user._id);
  const review = await Seller.findOne({
    _id: seller_id,
    reviews: { $elemMatch: { by: user_id } },
  });
  res.status(200).send(review);
} catch (err) {
  //sends the status code with error code message to the user
  res.status(502).send({
    error: 'Error retreiving review.',
  });
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!