2

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

Ankit Shah
  • 1,087
  • 2
  • 15
  • 30
  • 1
    You can check with https://www.npmjs.com/package/mongoose-deep-populate – Vikas Keskar Feb 22 '21 at 06:48
  • Check: https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#join-conditions-and-uncorrelated-sub-queries – Dheemanth Bhat Feb 22 '21 at 07:00
  • @VikasKeskar Thanks a lot, that helped. But just wondering if there is any way to populate data without using any other third-party packages. – Ankit Shah Feb 22 '21 at 07:04
  • @DheemanthBhat. I looked into it, but is there any way to use it in mongoose? – Ankit Shah Feb 22 '21 at 07:05
  • Check this: https://mongoosejs.com/docs/api/aggregate.html#aggregate_Aggregate-lookup or you can always write raw query in your manager file. Check here: https://stackoverflow.com/questions/39912017/mongodb-aggregate-query-with-lookup – Dheemanth Bhat Feb 22 '21 at 07:08
  • Have you tried populating nested properties using dot notation. In your `property` populate `path` try `'images propertyby amenities propertyCategory propertyLocation.city propertyLocation.area'` – v1shva Feb 22 '21 at 09:04
  • @v1shva. yes tried but with no success. – Ankit Shah Feb 22 '21 at 15:22

0 Answers0