繁体   English   中英

如何使用 Mongoose.populate - 它不起作用

[英]How can I use Mongoose.populate - it doesn't work

有人可以帮我吗? 我有两个模式和模型(我添加了完整版本的代码以明确):

const HouseSchema: Schema = new Schema(
    {
        name: {type: Schema.Types.String, required: true},
        class: {type: KeyDisplayNameSchema},
        levels: {type: Schema.Types.Number},
        price: {type: Schema.Types.Number, required: true},
        beginDate: {type: Schema.Types.String},
        squarePrice: {type: Schema.Types.Number},
        order: {type: Schema.Types.Number, default: 0},
        parking: {type: Schema.Types.Boolean},
        endDate: {type: Schema.Types.String},
        visibleInCarousel: {type: Schema.Types.Boolean, default: true},
        isDeleted: {type: Schema.Types.Boolean, default: false},
        apartmentComplex: {
            type: Schema.Types.ObjectId,
            ref: 'ApartmentComplex'
        },
        images: {
            type: imagesSchema,
            default: () => {
                return {};
            }
        },
        publishedDate: {type: Schema.Types.String}
    },
    {
        toJSON: {virtuals: true},
        toObject: {virtuals: true}
    }
);

HouseSchema.virtual('sections', {
    ref: 'Section',
    localField: '_id',
    foreignField: 'house'
});

HouseSchema.virtual('flats', {
    ref: 'Flat',
    localField: '_id',
    foreignField: 'house'
});

HouseSchema.virtual('layouts', {
    ref: 'HouseLayout',
    localField: '_id',
    foreignField: 'house'
});

HouseSchema.virtual('levelLayouts', {
    ref: 'LevelLayout',
    localField: '_id',
    foreignField: 'house'
});

HouseSchema.virtual('parkingComplex', {
    ref: 'ParkingComplex',
    localField: '_id',
    foreignField: 'houses'
});

const HouseModel = mongoose.model<House>('House', HouseSchema);
export default HouseModel;

const ParkingComplexSchema: Schema = new Schema(
    {
        name: {type: Schema.Types.String, required: true},
        address: {type: Schema.Types.String},
        city: {type: KeyDisplayNameSchema, required: true},
        district: {type: KeyDisplayNameSchema, required: true},
        undergroundStation: {type: KeyDisplayNameSchema},
        levels: {type: Schema.Types.Number, required: true},
        beginDate: {type: Schema.Types.String, required: true},
        endDate: {type: Schema.Types.String},
        isDeleted: {type: Schema.Types.Boolean, default: false},
        houses: [
            {
                type: Schema.Types.ObjectId,
                ref: 'House',
                default: () => {
                    return [];
                }
            }
        ],
        developer: {
            type: Schema.Types.ObjectId,
            ref: 'Developer'
        },
        apartmentComplex: {
            type: Schema.Types.ObjectId,
            ref: 'ApartmentComplex'
        }
    },
    {
        toJSON: {virtuals: true},
        toObject: {virtuals: true}
    }
);

ParkingComplexSchema.virtual('parkingPlaces', {
    ref: 'ParkingPlace',
    localField: '_id',
    foreignField: 'parkingComplexId'
});

const ParkingComplexModel = mongoose.model<ParkingComplex>('ParkingComplex', ParkingComplexSchema);

export default ParkingComplexModel;

这些模型彼此有参考。

然后我想得到停车场并填充字段“房屋”

如果我这样做:

getParkingComplex: async (parent, {uuid}) => {
        const data = await ParkingComplexModel.findById(uuid).exec()
        console.log(data)
        return data;
    }

我得到了停车场,并且字段“房屋”有两个房屋的 objectId:

{
  isDeleted: false,
  houses: [ 60aaa827ec89de07e0fb8db1, 60aaa827ec89de07e0fb8db2 ],
  _id: 60aaa827ec89de07e0fb8db3,
  levels: 2,
  beginDate: '2021-09-23T19:07:45.190Z',
  endDate: '2023-05-23T19:07:45.194Z',
  developer: 5ecd1c2590de761738c029a3,
  apartmentComplex: 5ecd20b290de761738c029ad,
  __v: 0,
  id: '60aaa827ec89de07e0fb8db3'
}

但如果我这样做:

 getParkingComplex: async (parent, {uuid}) => {
            const data = await ParkingComplexModel.findById(uuid)
             .populate('houses')
             .exec()
            console.log(data)
            return data;
        }

我得到空的“房屋”数组:

{
  isDeleted: false,
  houses: [],
  _id: 60aaa827ec89de07e0fb8db3,
  levels: 2,
  beginDate: '2021-09-23T19:07:45.190Z',
  endDate: '2023-05-23T19:07:45.194Z',
  developer: 5ecd1c2590de761738c029a3,
  apartmentComplex: 5ecd20b290de761738c029ad,
  __v: 0,
  id: '60aaa827ec89de07e0fb8db3'
}

我做错了什么? 感谢您的帮助!

你会不会……因为你将房屋的默认返回值设置为一个空数组?

...
houses: [
            {
                type: Schema.Types.ObjectId,
                ref: 'House',
                default: () => {
                    return [];
                }
            }
        ],
....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM