.where()由填充字段组成

时间:2015-12-15 06:18:16

标签: mongodb mongoose mongoose-populate

我想在他所在地区找到最接近具有特定技能的工人。

位置架构:

var schema = Schema({
    name: String,
    type: String, // home | latest
    user: {type: Schema.Types.ObjectId, ref: 'User'},
    address: String,
    position: {
        type: {type: String, default: 'Point'},
        coordinates: [Number]
    },
    status: String // active | inactive
}, {collection: 'locations'});

工作者架构:

var schema = Schema({
    username: String,
    password: String,
    firstName: {type: String, default: ''},
    middleName: {type: String, default: ''},
    lastName: {type: String, default: ''},
    role: {type: String, default: 'user'}, // admin | user | worker
    skills: [{
        object: {type: Schema.Types.ObjectId, ref: 'Skill'},
        slug: String, // Should remove in the future
        ratePerHour: Number,
        status: {type: String, default: 'active'} // active | inactive
    }],
    locations: [{type: Schema.Types.ObjectId, ref: 'Location'}]
}, {collection: 'users'});

技能架构:

var schema = Schema({
    name: String,
    slug: String,
    users: [{type: Schema.Types.ObjectId, ref: 'User'}],
    isFeatured: Boolean,
    minRatePerHour: Number,
    maxRatePerHour: Number,
    status: String // active | inactive | deleted
}, {collection: 'skills'});

Bellow是我的查询,但.where()不适用于填充字段。

Location
    .find({
        'position': {
            $near: {
                $geometry: {type: 'Point', coordinates: [lat, lng]},
                $minDistance: 0,
                $maxDistance: 20000
            }
        }
    })
    .populate('user')
    .deepPopulate('user.skills.object')
    .where({'user.skills': {$elemMatch: {'object.slug': 'cleaning'}}})
    .limit(5)
    .exec(callback);

我对这些架构做错了吗?我应该使用嵌入文档而不是ID引用吗? 还有什么方法可以查询吗?

1 个答案:

答案 0 :(得分:0)

您必须使用$?的特殊属性,如下所述:Query conditions and other options

所以我认为你可以用这样的结果得到你的结果:

populate

未经测试,请仅将其作为示例。