猫鼬的复杂查询,包括子文档,接近条件,
Complex query with mongoose, including subdocuments, near condition,
我有一个使用 Mongoose 的非常(至少对我而言)复杂的查询。
首先是我的架构:
var ObjectSchema = new Schema({
pickupStartDate: {type: Date, required: true, default: Date},
pickupEndDate: {type: Date, required: true, default: Date},
...
handoverStartDate: {type: Date, required: true, default: Date},
handoverEndDate: {type: Date, required: true, default: Date},
...
});
通过使用 "plugin mechanism" 我的对象有两个地址(称为 pickupAddress
和 handoverAddress
。地址看起来像这样:
var name = 'address';
var obj = {};
obj[name] = {
street: String,
zipCode: String,
city: String,
state: String,
country: String,
loc: {type: [Number], index: '2dsphere'}
};
schema.add(obj);
和另一个模式:
var TripSchema = new Schema({
startDate: {type: Date, required: true, default: Date},
endDate: {type: Date, required: true, default: Date},
handoverRadius: {type: Number, required: true}
});
它也有一个address
(再次使用插件机制)。
我想要以下查询:
查找所有 "objects" 其中 "fit" 到我的行程。
"Fit" 表示:
handoverStartDate >= trip.startDate
handoverEndDate <= trip.endDate
- `handoverAddress 靠近 trip.address
- ...
我认为这是一个很好的方法:
ObjectSchema
.find()
.and([
{ handoverStartDate: {$gte: trip.startDate}},
{ handoverEndDate: {$lte: trip.endDate}},
{ 'handoverAddress.loc': {$near: {
'$maxDistance': 10 * 1000,
'$center': {
type: 'Point',
coordinates: trip.address.loc
}
}}}
])
.exec(function(err, cdObjects) {
console.log(err);
console.log(cdObjects);
});
但这会导致以下错误:
{ message: 'Cast to number failed for value "[object Object]" at path "handoverAddress.loc"'
.
我猜是因为 'handoverAddress.loc'
。但我不确定如何指定它,因为它必须是一个字符串(因为它是一个子文档)。
您不需要 and。尝试
ObjectModel.find({
handoverStartDate: {$gte: trip.startDate},
handoverEndDate: {$lte: trip.endDate},
'handoverAddress.loc': {
$near: {
$geometry: {
type: "Point",
coordinates: trip.address.loc
},
$maxDistance: 10 * 1000
}
})
确保 trip 被定义为一个变量,并且 startDate、endDate 和 address 都是符合您期望的已定义属性。
这对我来说是这样的:
ObjectSchema
.where('handoverStartDate').gte(trip.startDate)
.where('handoverEndDate').lte(trip.endDate)
.where('handoverAddress.loc').near({
center: {
type: 'Point',
coordinates: trip.address.loc
},
maxDistance: 10 * 1000
});
我有一个使用 Mongoose 的非常(至少对我而言)复杂的查询。
首先是我的架构:
var ObjectSchema = new Schema({
pickupStartDate: {type: Date, required: true, default: Date},
pickupEndDate: {type: Date, required: true, default: Date},
...
handoverStartDate: {type: Date, required: true, default: Date},
handoverEndDate: {type: Date, required: true, default: Date},
...
});
通过使用 "plugin mechanism" 我的对象有两个地址(称为 pickupAddress
和 handoverAddress
。地址看起来像这样:
var name = 'address';
var obj = {};
obj[name] = {
street: String,
zipCode: String,
city: String,
state: String,
country: String,
loc: {type: [Number], index: '2dsphere'}
};
schema.add(obj);
和另一个模式:
var TripSchema = new Schema({
startDate: {type: Date, required: true, default: Date},
endDate: {type: Date, required: true, default: Date},
handoverRadius: {type: Number, required: true}
});
它也有一个address
(再次使用插件机制)。
我想要以下查询:
查找所有 "objects" 其中 "fit" 到我的行程。 "Fit" 表示:
handoverStartDate >= trip.startDate
handoverEndDate <= trip.endDate
- `handoverAddress 靠近 trip.address
- ...
我认为这是一个很好的方法:
ObjectSchema
.find()
.and([
{ handoverStartDate: {$gte: trip.startDate}},
{ handoverEndDate: {$lte: trip.endDate}},
{ 'handoverAddress.loc': {$near: {
'$maxDistance': 10 * 1000,
'$center': {
type: 'Point',
coordinates: trip.address.loc
}
}}}
])
.exec(function(err, cdObjects) {
console.log(err);
console.log(cdObjects);
});
但这会导致以下错误:
{ message: 'Cast to number failed for value "[object Object]" at path "handoverAddress.loc"'
.
我猜是因为 'handoverAddress.loc'
。但我不确定如何指定它,因为它必须是一个字符串(因为它是一个子文档)。
您不需要 and。尝试
ObjectModel.find({
handoverStartDate: {$gte: trip.startDate},
handoverEndDate: {$lte: trip.endDate},
'handoverAddress.loc': {
$near: {
$geometry: {
type: "Point",
coordinates: trip.address.loc
},
$maxDistance: 10 * 1000
}
})
确保 trip 被定义为一个变量,并且 startDate、endDate 和 address 都是符合您期望的已定义属性。
这对我来说是这样的:
ObjectSchema
.where('handoverStartDate').gte(trip.startDate)
.where('handoverEndDate').lte(trip.endDate)
.where('handoverAddress.loc').near({
center: {
type: 'Point',
coordinates: trip.address.loc
},
maxDistance: 10 * 1000
});