I am trying to get ObjectID data of ObjectID referenced in a Mongoose Model. I have two main Model Property and Listing. Listing has an Object of Property and Property contains Object of many other models. I want to fetch all Objects of Property along with Property in my Listing. This is my schema for Property
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const slug = require('mongoose-slug-generator');
mongoose.plugin(slug);
const PropertySchema = Schema({
propertyby: {
type: Schema.Types.ObjectId,
ref: 'user',
},
propertyName: {
type: String,
required: true,
max: 100,
},
propertySlug: {
type: String,
slug: 'propertyName',
slug_padding_size: 2,
unique: true,
},
propertyLocation: {
address: {
type: String,
},
city: {
type: Schema.Types.ObjectId,
ref: 'city',
},
area: {
type: Schema.Types.ObjectId,
ref: 'area',
},
zipCode: {
type: String,
},
latitude: {
type: String,
},
longitude: {
type: String,
},
},
propertyCategory: {
type: Schema.Types.ObjectId,
ref: 'propertycategory',
},
propertyType: {
type: String,
enum: ['Resedential', 'Commercial', 'Industrial', 'Agricultural'],
default: 'Resedential',
},
propertyFace: {
type: String,
enum: [
'Default',
'North',
'East',
'South',
'West',
'South East',
'South West',
'North East',
'North West',
],
default: 'Default',
},
bedroom: {
type: Number,
default: '0',
},
masterBedroom: {
type: Number,
default: '0',
},
bathroom: {
type: Number,
default: '0',
},
attachedBathroom: {
type: Number,
default: '0',
},
balcony: {
type: Number,
default: '0',
},
hall: {
type: Number,
default: '0',
},
dining: {
type: Number,
default: '0',
},
floor: {
type: Number,
default: '0',
},
bikeParking: {
type: Number,
default: '0',
},
carParking: {
type: Number,
default: '0',
},
storeroom: {
type: Number,
default: '0',
},
guardRoom: {
type: Number,
default: '0',
},
roadWidth: {
type: Number,
},
roadSizePostfix: {
type: String,
enum: ['Feet', 'Meters'],
default: 'Feet',
},
roadType: {
type: String,
enum: ['Default', 'Paved', 'Gravelled', 'Stonized'],
default: 'Default',
},
builtYear: {
type: String,
},
carpetArea: {
type: String,
},
carpetAreaPostfix: {
type: String,
enum: [
'Sq. Feet',
'Sq. Meter',
'Aana',
'Ropani',
'Paisa',
'Daam',
'Dhur',
'Gaj',
'Bigha',
'Katha',
'Dhur',
'Acre',
],
default: 'Aana',
},
areaCovered: {
type: String,
},
areaCoveredPostfix: {
type: String,
enum: [
'Sq. Feet',
'Sq. Meter',
'Aana',
'Ropani',
'Paisa',
'Daam',
'Dhur',
'Gaj',
'Bigha',
'Katha',
'Dhur',
'Acre',
],
default: 'Aana',
},
amenities: [
{
type: Schema.Types.ObjectId,
ref: 'amenities',
},
],
description: [
{
des: {
type: String,
},
floorplan: [
{
floorcount: {
type: String,
},
bedroom: {
type: Number,
default: '0',
},
masterbedroom: {
type: Number,
default: '0',
},
balcony: {
type: String,
},
hall: {
type: Number,
default: '0',
},
bathroom: {
type: Number,
default: '0',
},
attachedBathroom: {
type: Number,
default: '0',
},
dining: {
type: Number,
default: '0',
},
},
],
nearestAttraction: [
{
attractionName: {
type: String,
},
description: {
type: Number,
default: '0',
},
latitude: {
type: String,
},
longitude: {
type: String,
},
},
],
},
],
images: [
{
type: Schema.Types.ObjectId,
ref: 'media',
},
],
});
PropertySchema.set('timestamps', true);
module.exports = Property = mongoose.model('property', PropertySchema);
And this is my Schema for Listing
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const PropertyListingSchema = Schema({
property: {
type: Schema.Types.ObjectId,
ref: 'property',
},
listedby: {
type: Schema.Types.ObjectId,
ref: 'user',
},
listPrice: {
type: String,
},
listPriceUnit: {
type: String,
enum: [
'Onetime',
'Per Month',
'Per Day',
'Per Annual',
'Per Sq. Feet',
'Per Sq. Meter',
'Per Aana',
'Per Ropani',
'Per Dhur',
'Per Gaj',
'Per Katha',
'Per Acre',
'Expense Sharing',
],
},
listType: {
type: String,
enum: ['Buy', 'Rent', 'Lease', 'Share'],
},
contactNumber: [
{
type: String,
},
],
specialNote: {
type: String,
},
numOfViewers: {
type: String,
},
});
PropertyListingSchema.set('timestamps', true);
module.exports = PropertyListing = mongoose.model(
'propertylisting',
PropertyListingSchema
);
I am trying to get all data of property like category, images, amenities, city and area inside propertyLocation of property. The code I am running to get some insufficient data is:
const getListing = await PropertyListing.findOne({
_id: propertyListing._id,
})
.populate('listedby')
.populate({
path: 'property',
populate: {
path: 'images propertyby amenities propertyCategory',
},
});
But this is getting me data of only images, propertyby, amenities. How can I get Data of City and Area in Property Location