mongoose virtual population not working correctly

I’ve check all related questions on stackoverflow and also visit the mongoose docs, everything seems to be good. But the problem is when i’m trying to get tour by id.

  1. if i don’t add populate to end of query inside the getTourById, then it should give me reviews as null. but it did’nt give me anything.
  2. when i added pupulate to end of query, it it gave me an array of all reviews. here i got confused why it gave me all reviews? as i have 7 tours in DB. why it repeat the same review 7 times and only one review gave me required result.

tourSchema code:

import mongoose, { mongo } from 'mongoose';
import slugify from 'slugify';
import { User } from './user.model.js';

const tourSchema = mongoose.Schema(
  {
    name: {
      type: String,
      required: true,
      unique: true,
      trim: true,
      maxLength: [40, 'A tour name must have equal or less than 40 characters'],
      minLength: [10, 'A tour name must have equal or more than 10 characters']
    },
    slug: String,
    duration: {
      type: Number,
      required: [true, 'A tour must have a duration']
    },
    secretTour: {
      type: Boolean,
      default: false
    },
    maxGroupSize: {
      type: Number,
      required: [true, 'A tour must have a group size']
    },
    price: {
      type: Number,
      required: [true, 'A tour must have a price']
    },
    difficulty: {
      type: String,
      required: [true, 'A tour must have difficulty level'],
      enum: {
        values: ['easy', 'medium', 'difficult'],
        message: 'Difficulty can be either easy, medium or difficult'
      }
    },
    ratingsAverage: {
      type: Number,
      default: 4.2,
      min: [1, 'Rating must be above 1.0'],
      max: [5, 'Rating must be below 5.0']
    },
    ratingsQuantity: {
      type: Number,
      default: 0
    },
    priceDiscount: {
      type: Number,
      validate: function (val) {
        return val < this.price;
      },
      message: 'Discount price {VALUE} should be less than regular price'
    },
    summary: {
      type: String,
      trim: true,
      required: [true, 'A tour must have a summary']
    },
    description: {
      type: String,
      trim: true
    },
    imageCover: {
      type: String,
      required: [true, 'A tour must have a cover image']
    },
    images: [String],
    startDates: [Date],
    startLocation: {
      type: {
        type: String,
        default: 'Point',
        enum: ['Point']
      },
      coordinates: [Number],
      address: String,
      description: String
    },
    locations: [
      {
        type: {
          type: String,
          default: 'Point',
          enum: ['Point']
        },
        coordinates: [Number],
        address: String,
        description: String,
        day: Number
      }
    ],
    guides: [
      {
        type: mongoose.Schema.ObjectId,
        ref: 'User'
      }
    ]
  },
  {
    timestamps: true,
    toJSON: { virtuals: true }, 
    toObject: { virtuals: true }
  }
);

tourSchema.virtual('durationInWeeks').get(function () {
  return this.duration / 7;
});

// Virtually population
tourSchema.virtual('reviews', {
  ref: 'Review',
  localField: '_id',
  foreignField: 'tour' 
});
export const Tour = mongoose.model('Tour', tourSchema);

tourController code:

const getTour = asyncHandler(async (req, res, next) => {
  const tour = await Tour.findById(req.params.id).populate('reviews');
  if (!tour) {
    return next(new appError('Tour not found for that ID', 404));
  }
  res.status(200).json({
    message: 'success',
    requestedAt: req.requestTime,
    data: {
      tour
    }
  });
});

reviewModel code:

    import mongoose from 'mongoose';
    
    const reviewSchema = mongoose.Schema(
      {
        review: {
          type: String,
          required: [true, "Can't post an empty review"]
        },
        rating: {
          type: Number,
          min: 1,
          max: 5
        },
        tour: {
          type: mongoose.Schema.Types.ObjectId,
          ref: 'Tour',
          required: [true, 'Review must belong to a tour']
        },
        user: {
          type: mongoose.Schema.Types.ObjectId,
          ref: 'User',
          required: [true, 'Review must belong to a user']
        }
      },
      { timestamps: true, toJSON: { virtuals: true }, toObject: { virtuals: true } }
    );
    
    reviewSchema.pre(/^find/, function (next) {
      this.populate({
        path: 'tour',
        options: { select: 'name' }
      }).populate({
        path: 'user',
        options: { select: 'name photo' }
      });
      next();
    });
    
    export const Review = mongoose.model('Review', reviewSchema);


reviewController code: 

import { Review } from '../models/review.model.js';
import { appError } from '../utils/appError.js';
import { asyncHandler } from '../utils/asyncHandler.js';

const getAllReviews = asyncHandler(async (req, res, next) => {
  const reviews = await Review.find();
  res.status(200).json({
    status: 'success',
    results: reviews.length,
    data: {
      reviews
    }
  });
});

const addNewReview = asyncHandler(async (req, res, next) => {
  const review = await Review.create(req.body);
  if (!review) return next(new appError('Failed to add review. Please try again', 403));
  res.status(201).json({
    status: 'success',
    data: {
      review
    }
  });
});

export { getAllReviews, addNewReview };

Output after sending request for a tour by id:

{
    "message": "success",
    "requestedAt": "2024-05-07T11:42:37.442Z",
    "data": {
        "tour": {
            "startLocation": {
                "type": "Point",
                "coordinates": [
                    -106.822318,
                    39.190872
                ],
                "address": "419 S Mill St, Aspen, CO 81611, USA",
                "description": "Aspen, USA"
            },
            "_id": "5c88fa8cf4afda39709c295a",
            "name": "The Snow Adventurer",
            "duration": 4,
            "secretTour": false,
            "maxGroupSize": 10,
            "price": 997,
            "difficulty": "difficult",
            "ratingsAverage": 4.5,
            "ratingsQuantity": 6,
            "summary": "Exciting adventure in the snow with snowboarding and skiing",
            "description": "Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua, ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum!nDolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur, exercitation ullamco laboris nisi ut aliquip. Lorem ipsum dolor sit amet, consectetur adipisicing elit!",
            "imageCover": "tour-3-cover.jpg",
            "images": [
                "tour-3-1.jpg",
                "tour-3-2.jpg",
                "tour-3-3.jpg"
            ],
            "startDates": [
                "2022-01-05T10:00:00.000Z",
                "2022-02-12T10:00:00.000Z",
                "2023-01-06T10:00:00.000Z"
            ],
            "locations": [
                {
                    "type": "Point",
                    "coordinates": [
                        -106.855385,
                        39.182677
                    ],
                    "description": "Aspen Highlands",
                    "day": 1,
                    "_id": "5c88fa8cf4afda39709c295c",
                    "id": "5c88fa8cf4afda39709c295c"
                },
                {
                    "type": "Point",
                    "coordinates": [
                        -106.516623,
                        39.60499
                    ],
                    "description": "Beaver Creek",
                    "day": 2,
                    "_id": "5c88fa8cf4afda39709c295b",
                    "id": "5c88fa8cf4afda39709c295b"
                }
            ],
            "guides": [],
            "createdAt": "2024-05-07T05:58:58.291Z",
            "updatedAt": "2024-05-07T05:58:58.291Z",
            "slug": "The-Snow-Adventurer",
            "__v": 0,
            "durationInWeeks": 0.5714285714285714,
            "reviews": [
                {
                    "_id": "5c8a37b114eb5c17645c9111",
                    "review": "Ex a bibendum quis volutpat consequat euismod vulputate parturient laoreet diam sagittis amet at blandit.",
                    "rating": 4,
                    "tour": {
                        "_id": "5c88fa8cf4afda39709c295a",
                        "name": "The Snow Adventurer",
                        "guides": [],
                        "durationInWeeks": null,
                        "id": "5c88fa8cf4afda39709c295a"
                    },
                    "user": null,
                    "createdAt": "2024-05-07T06:01:10.946Z",
                    "updatedAt": "2024-05-07T06:01:10.946Z",
                    "__v": 0,
                    "id": "5c8a37b114eb5c17645c9111"
                },
                {
                    "_id": "5c8a382d14eb5c17645c9116",
                    "review": "Semper blandit felis nostra facilisi sodales pulvinar habitasse diam sapien lobortis urna nunc ipsum orci.",
                    "rating": 5,
                    "tour": {
                        "_id": "5c88fa8cf4afda39709c295a",
                        "name": "The Snow Adventurer",
                        "guides": [],
                        "durationInWeeks": null,
                        "id": "5c88fa8cf4afda39709c295a"
                    },
                    "user": null,
                    "createdAt": "2024-05-07T06:01:10.946Z",
                    "updatedAt": "2024-05-07T06:01:10.946Z",
                    "__v": 0,
                    "id": "5c8a382d14eb5c17645c9116"
                },
                {
                    "_id": "5c8a355b14eb5c17645c9109",
                    "review": "Tempus curabitur faucibus auctor bibendum duis gravida tincidunt litora himenaeos facilisis vivamus vehicula potenti semper fusce suspendisse sagittis!",
                    "rating": 4,
                    "tour": {
                        "_id": "5c88fa8cf4afda39709c295a",
                        "name": "The Snow Adventurer",
                        "guides": [],
                        "durationInWeeks": null,
                        "id": "5c88fa8cf4afda39709c295a"
                    },
                    "user": null,
                    "createdAt": "2024-05-07T06:01:10.946Z",
                    "updatedAt": "2024-05-07T06:01:10.946Z",
                    "__v": 0,
                    "id": "5c8a355b14eb5c17645c9109"
                },
                {
                    "_id": "5c8a3a9914eb5c17645c9126",
                    "review": "Netus eleifend adipiscing ligula placerat fusce orci sollicitudin vivamus conubia.",
                    "rating": 5,
                    "tour": {
                        "_id": "5c88fa8cf4afda39709c295a",
                        "name": "The Snow Adventurer",
                        "guides": [],
                        "durationInWeeks": null,
                        "id": "5c88fa8cf4afda39709c295a"
                    },
                    "user": null,
                    "createdAt": "2024-05-07T06:01:10.947Z",
                    "updatedAt": "2024-05-07T06:01:10.947Z",
                    "__v": 0,
                    "id": "5c8a3a9914eb5c17645c9126"
                },
                {
                    "_id": "5c8a3b9f14eb5c17645c9130",
                    "review": "Tempor pellentesque eu placerat auctor enim nam suscipit tincidunt natoque ipsum est.",
                    "rating": 5,
                    "tour": {
                        "_id": "5c88fa8cf4afda39709c295a",
                        "name": "The Snow Adventurer",
                        "guides": [],
                        "durationInWeeks": null,
                        "id": "5c88fa8cf4afda39709c295a"
                    },
                    "user": null,
                    "createdAt": "2024-05-07T06:01:10.948Z",
                    "updatedAt": "2024-05-07T06:01:10.948Z",
                    "__v": 0,
                    "id": "5c8a3b9f14eb5c17645c9130"
                },
                {
                    "_id": "5c8a3c3b14eb5c17645c9135",
                    "review": "Massa orci lacus suspendisse maximus ad integer donec arcu parturient facilisis accumsan consectetur non",
                    "rating": 4,
                    "tour": {
                        "_id": "5c88fa8cf4afda39709c295a",
                        "name": "The Snow Adventurer",
                        "guides": [],
                        "durationInWeeks": null,
                        "id": "5c88fa8cf4afda39709c295a"
                    },
                    "user": null,
                    "createdAt": "2024-05-07T06:01:10.948Z",
                    "updatedAt": "2024-05-07T06:01:10.948Z",
                    "__v": 0,
                    "id": "5c8a3c3b14eb5c17645c9135"
                },
                {
                    "_id": "663a11fba58abfda2626679b",
                    "review": "This review sample  ",
                    "rating": 5,
                    "tour": {
                        "_id": "5c88fa8cf4afda39709c295a",
                        "name": "The Snow Adventurer",
                        "guides": [],
                        "durationInWeeks": null,
                        "id": "5c88fa8cf4afda39709c295a"
                    },
                    "user": {
                        "_id": "6639cbfcbec906df9298a897",
                        "name": "Guest User"
                    },
                    "createdAt": "2024-05-07T11:35:23.399Z",
                    "updatedAt": "2024-05-07T11:35:23.399Z",
                    "__v": 0,
                    "id": "663a11fba58abfda2626679b"
                }
            ],
            "id": "5c88fa8cf4afda39709c295a"
        }
    }
}

I tried whatever possible from my side. also here i tried to give all necessary code to check. please point out the issue and help me.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật