Mongoose按附件查找筛选

Mongoose按附件查找筛选,mongoose,find,Mongoose,Find,我正在努力使用mongoose和nodejs进行查询。我正在尝试从提供商处获取所有预订,此提供商通过包文档与预订文档相关。 我有以下预订模式: const bookingSchema = new Schema( { package: { type: Schema.Types.ObjectId, ref: 'Package', required: [ true, 'A reference to a package must exist']

我正在努力使用mongoose和nodejs进行查询。我正在尝试从提供商处获取所有预订,此提供商通过包文档与预订文档相关。 我有以下预订模式:

const bookingSchema = new Schema( {

    package: {
        type: Schema.Types.ObjectId,
        ref: 'Package',
        required: [ true, 'A reference to a package must exist']
    },


    requesters: {
        type: Number,
        default: 1
    },

    status: {
        type: String
    },

}); 
这是包模式:

const packageSchema = new Schema( {


    title: {
        type: String
    },


    profile: {
        type: Schema.Types.ObjectId,
        ref: 'Profile',
        required: [ true, 'A reference to a profile must exist']
    },

    service: {
        type: Schema.Types.ObjectId,
        ref: 'Service',
        required: [ true, 'A reference to a service must exist']
    },

});
现在,我正在尝试使用以下配置文件和服务获取该提供商的所有预订:

 const filter = { 
        $and: [
        {"package" : { $exists: true } }, 
        {"package.profile": ObjectId(profile._id)}
        ]
    };


return Booking.find( filter )
            .populate({
                path: 'package',
                match: {'profile' : { $exists: true}},
                model: 'Package',
                select : 'profile service',

                populate : [
                    {
                    path : 'profile',
                    model: 'Profile',
                    select : '_id name user'

                    
                    },               
                ]
            })
            .populate(
                {
                    path : 'service',
                    model: 'Service',
                    select : '_id'
                }
                 
            )
            .sort( { date: -1 } )
            .exec();
但我得到的是一个空数组,不是结果。这是我可以在控制台中看到的查询

Mongoose: bookings.find({ '$and': [ { package: { '$exists': true } }, { 'package.profile': ObjectId("5f2a668ccee9bd8fd7a75d29") } ]}, { sort: { date: -1 }, projection: {} })
bookings []
我只需要此提供商的预订,其中profile.\u id等于我传递给find方法的提供商profile。


事先非常感谢。

最后,我通过以下方式解决了问题:

 const packages = await Package.find({'profile' : profile._id}).select('_id').exec();

    const ids: any = packages.map( (pkg: any) => {
        return pkg._id;
    });

 return Booking.find( {'package': {$in: ids}} ) .....
还有更好的解决办法吗