Mongoose/mongodb - 只获取每个 id 的最新记录
Mongoose/mongodb - get only latest records per id
我在猫鼬中有一个检查模型:
var InspectionSchema = new Schema({
business_id: {
type: String,
required: true
},
score: {
type: Number,
min: 0,
max: 100,
required: true
},
date: {
type: Number, // in format YYYYMMDD
required: true
},
description: String,
type: String
});
InspectionSchema.index({business_id: 1, date: 1}, {unique: true});
可以对同一商家进行多次检查(每个商家都由唯一的 business_id 表示)。但是,每个企业每天只能检查一次,这就是为什么在 business_id + date.
上有唯一索引的原因
我还在 Inspection 对象上创建了一个静态方法,给定一个 business_id 列表,它检索基础业务的所有检查。
InspectionSchema.statics.getAllForBusinessIds = function(ids, callback) {
this.find({'business_id': {$in: ids}}, callback);
};
此函数获取所请求企业的所有检查。但是,我还想创建一个函数,根据 business_id.
仅获取 最新的 检查
InspectionSchema.statics.getLatestForBusinessIds = function(ids, callback) {
// query to get only the latest inspection per business_id in "ids"?
};
我该如何实施?
您可以使用.aggregate()
方法在一次请求中获取所有最新数据:
Inspection.aggregate(
[
{ "$sort": { "buiness_id": 1, "date": -1 } },
{ "$group": {
"_id": "$business_id",
"score": { "$first": "$score" },
"date": { "$first": "$date" },
"description": { "$first": "$description" },
"type": { "$first": "$type" }
}}
],
function(err,result) {
}
);
只需 $sort
then $group
with the "business_id" as the grouping key. The $first
从分组边界获取第一个结果,我们已经在每个 id 中按日期排序。
如果您只想要日期,请使用 $max
:
Inspection.aggregate(
[
{ "$group": {
"_id": "$business_id",
"date": { "$max": "$date" }
}}
],
function(err,result) {
}
);
如果您想在执行此操作时 "pre-filter" 业务 ID 值或任何其他条件,另请参阅 $match
。
试试这个:
Inpection.aggregate(
[
{ $match : { _id : { "$in" : ids} } },
{ $group: { "_id" : "$business_id", lastInspectionDate: { $last: "$date" } } }
],
function(err,result) {
}
);
我在猫鼬中有一个检查模型:
var InspectionSchema = new Schema({
business_id: {
type: String,
required: true
},
score: {
type: Number,
min: 0,
max: 100,
required: true
},
date: {
type: Number, // in format YYYYMMDD
required: true
},
description: String,
type: String
});
InspectionSchema.index({business_id: 1, date: 1}, {unique: true});
可以对同一商家进行多次检查(每个商家都由唯一的 business_id 表示)。但是,每个企业每天只能检查一次,这就是为什么在 business_id + date.
上有唯一索引的原因我还在 Inspection 对象上创建了一个静态方法,给定一个 business_id 列表,它检索基础业务的所有检查。
InspectionSchema.statics.getAllForBusinessIds = function(ids, callback) {
this.find({'business_id': {$in: ids}}, callback);
};
此函数获取所请求企业的所有检查。但是,我还想创建一个函数,根据 business_id.
仅获取 最新的 检查InspectionSchema.statics.getLatestForBusinessIds = function(ids, callback) {
// query to get only the latest inspection per business_id in "ids"?
};
我该如何实施?
您可以使用.aggregate()
方法在一次请求中获取所有最新数据:
Inspection.aggregate(
[
{ "$sort": { "buiness_id": 1, "date": -1 } },
{ "$group": {
"_id": "$business_id",
"score": { "$first": "$score" },
"date": { "$first": "$date" },
"description": { "$first": "$description" },
"type": { "$first": "$type" }
}}
],
function(err,result) {
}
);
只需 $sort
then $group
with the "business_id" as the grouping key. The $first
从分组边界获取第一个结果,我们已经在每个 id 中按日期排序。
如果您只想要日期,请使用 $max
:
Inspection.aggregate(
[
{ "$group": {
"_id": "$business_id",
"date": { "$max": "$date" }
}}
],
function(err,result) {
}
);
如果您想在执行此操作时 "pre-filter" 业务 ID 值或任何其他条件,另请参阅 $match
。
试试这个:
Inpection.aggregate(
[
{ $match : { _id : { "$in" : ids} } },
{ $group: { "_id" : "$business_id", lastInspectionDate: { $last: "$date" } } }
],
function(err,result) {
}
);