1.只统计记录总数:
let result = await CorrectionRecordModel.countDocuments(db);
2.数组遍历,循环体中可以有调用异步函数:
for(let item of result2){
if(item && Tool.checkNotEmptString(item.auth_id) && (item.status !== 1)){
await apiUtil.modifyData(EstateDetailModel, {auth_id: item.auth_id}, {
status: 1,
});
}
}
3.遍历数组,循环体中不可以调用异步函数:
space_service_labels.forEach(item =>{
let name = Tool.getEnumValue(14, item);
if(Tool.checkNotEmptString(name)){
space_service_labels_names.push(name);
}
});
4.对数组,投影出指定对象组成新数组:
let ids = list.map(item => item.id);
5.查询出指定距离内的记录:
db.location = {
$near: {
$geometry: currentLocation,
$maxDistance: distance1
}
};
6.对查询出的结果数组,按照指定字段再排序:
// 按照每组内的记录数降序排序
groupedArray.sort((a, b) => b.count - a.count);
7.数据查询条件中对结果进行排序(-1从倒序,1正序):
db.sort({ update_time : -1 })
8.聚合语句:
let result = await ApiUserPushModel.aggregate([
{
$match: {
user_id:,
create_time: {
$gte: start_time,
$lte: end_time
},
}
},
{
$group: {
_id: '$batch_number',
push_count: { $sum: 1 },
push_title: { $first: '$push_title' },
commission : { $first: '$commission' },
notes : { $first: '$notes' },
create_time : { $first: '$create_time' },
batch_number : { $first: '$batch_number' },
data: { $push: '$$ROOT' },
}
},
{
$project:{
_id:0
}
}
]);
-
查询是否存在:
const exist = await UserModel.exists({ user_name: user_name });