relidon
relidon

Reputation: 2292

How to select a mongo subdocument by id with mongoose?

I'm going through a book and the code doesn't work. Everything in google and here indicated that the code is right! It doesn't work

Loc
      .findById(req.params.locationid)
      .select('name reviews')
      .exec(
        function(err, location) {
          if (location.reviews && location.reviews.length > 0) {
            // this is the problem:
            review = location.reviews.id(req.params.reviewid); 
            if (!review) {
              sendJSONresponse(res, 404, {
                "message": "reviewid not found"
              });
            } else {
              response = {
                location: {
                  name: location.name,
                  id: req.params.locationid
                },
                review: review
              };
              sendJSONresponse(res, 200, response);
            }
          }
        }
    );

The id is not a function, so id() returns nothing.

I tried hard coding the id number, tired many things that I found online, but the bottom line is that location.reviews.id is just a value. I also did Loc.findById(req.params.locationid).reviews ...: where, findById, nothing! I also waisted a lot of time trying _id and many things that went with that

I could loop through the reviews and stop if the reviews.id match, but I want to know what the author was trying to do.

The way I understand it is that location is a javascript object, it's natural not to be able to use the id as a function

This is the schema

var reviewSchema = new mongoose.Schema({
  author: String,
  rating: {type: Number, required: true, min: 0, max: 5},
  reviewText: String,
  createdOn: {type: Date, "default": Date.now}
});

var locationSchema = new mongoose.Schema({ 
    name: {type: String, required: true},
  address: String,
  rating: {type: Number, "default": 0, min: 0, max: 5},
  facilities: [String],
  coords: {type: [Number], index: '2dsphere'},
  openingTimes: [openingTimeSchema], //nesting a schema
  reviews: [reviewSchema]
});

The entire module code is (I though I'd share it incase I didn't select the right chunk):

module.exports.reviewsReadOne = function(req, res) {
  console.log("Getting single review");
  if (req.params && req.params.locationid && req.params.reviewid) {
    Loc
      .findById(req.params.locationid)
      .select('name reviews')
      .exec(
        function(err, location) {
          if (location.reviews && location.reviews.length > 0) {
            review = location.reviews.id(req.params.reviewid);
            if (!review) {
              sendJSONresponse(res, 404, {
                "message": "reviewid not found"
              });
            } else {
              response = {
                location: {
                  name: location.name,
                  id: req.params.locationid
                },
                review: review
              };
              sendJSONresponse(res, 200, response);
            }
          }
        }
    );
  } else {
    sendJSONresponse(res, 404, {
      "message": "Not found, locationid and reviewid are both required"
    });
  }
};

Than you help?

Upvotes: 1

Views: 880

Answers (2)

Dan Schwartz
Dan Schwartz

Reputation: 79

I recognize this as the example on page 151 of Getting MEAN by Simon Holmes. The problem is that there is a tiny error in the book. The name "id" in the above $push instruction should be "_id". Make this change and everything works. The above suggestion that you simply delete id: ObjectId() is incorrect.

Upvotes: 1

zangw
zangw

Reputation: 48386

Per our discussion, we found the root cause by

db.locations.update({ name: 'Starcups' }, 
                    { $push: { 
                         reviews: { 
                            author: 'Simon Holmes', 
                            id: ObjectId(), // issue is here
                            rating: 5, ... } } })

id: ObjectId() will create id field in the subdocument and without _id field in the reviews subdocument.

id() method is used to documentArrays have a special id method for looking up a document by its _id. Since there is no _id field in reviews document array, it does not work well.

Please remove id: ObjectId() from your code.

Upvotes: 1

Related Questions