Node.js 如何在mongoose中填充ObjectID的ObjectID数据
我试图获取Mongoose模型中引用的ObjectID的ObjectID数据。 我有两个主要的模型属性和列表。清单中有一个Property对象,Property包含许多其他模型的对象。我想获取所有属性对象以及列表中的属性。 这是我的属性模式Node.js 如何在mongoose中填充ObjectID的ObjectID数据,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我试图获取Mongoose模型中引用的ObjectID的ObjectID数据。 我有两个主要的模型属性和列表。清单中有一个Property对象,Property包含许多其他模型的对象。我想获取所有属性对象以及列表中的属性。 这是我的属性模式 const mongoose = require('mongoose'); const Schema = mongoose.Schema; const slug = require('mongoose-slug-generator'); mongoose.p
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);
这是我的清单模式
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
);
我试图获得所有的财产数据,如类别,图像,设施,城市和地区内的财产定位。
我正在运行以获取一些不足数据的代码是:
const getListing = await PropertyListing.findOne({
_id: propertyListing._id,
})
.populate('listedby')
.populate({
path: 'property',
populate: {
path: 'images propertyby amenities propertyCategory',
},
});
但这只给我提供了图像、房产和便利设施的数据。如何获取酒店位置中的城市和区域数据您可以通过检查进行检查:@VikasKeskar非常感谢,这很有帮助。但我只是想知道是否有任何方法可以在不使用任何其他第三方软件包的情况下填充数据。@DheemanthBhat。我研究过它,但有没有办法在mongoose中使用它?检查一下:或者您可以在manager文件中编写原始查询。请点击此处: