我想在他所在地区找到最接近具有特定技能的工人。
位置架构:
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引用吗? 还有什么方法可以查询吗?
答案 0 :(得分:0)