在 mongo 中分组,不包括空值
group in mongo excluding null values
我有 mongo 对文档进行分组操作的查询。
我几乎得到了预期的结果,除了我想在没有空值或空值的情况下优化结果。
目前我的查询是这样的:
db.productMetadata.aggregate([{$group:{"_id":{"color":"$productAttribute.colour","gender":"$productAttribute.gender"},"count" : {$sum : 1}}}]);
结果看起来像这样:
{ "_id" : { "color" : "BLUE", "gender" : "MEN" }, "count" : 1 }
{ "_id" : { }, "count" : 4 }
{ "_id" : { "color" : "NA", "gender" : "WOMEN" }, "count" : 1 }
{ "_id" : { "color" : "BLACK", "gender" : "MEN" }, "count" : 1 }
{ "_id" : { "color" : "BROWN", "gender" : "WOMEN" }, "count" : 1 }
{ "_id" : { "gender" : "MEN" }, "count" : 2 }
{ "_id" : { "color" : "BEIGE", "gender" : "MEN" }, "count" : 1 }
{ "_id" : { "color" : "BROWN", "gender" : "MEN" }, "count" : 1 }
如果在 DB 的实际数据中任何按字段分组的值是空的或 null,我想删除这些行。
例外结果应如下所示:
{ "_id" : { "color" : "BLUE", "gender" : "MEN" }, "count" : 1 }
{ "_id" : { "color" : "NA", "gender" : "WOMEN" }, "count" : 1 }
{ "_id" : { "color" : "BLACK", "gender" : "MEN" }, "count" : 1 }
{ "_id" : { "color" : "BROWN", "gender" : "WOMEN" }, "count" : 1 }
{ "_id" : { "color" : "BEIGE", "gender" : "MEN" }, "count" : 1 }
{ "_id" : { "color" : "BROWN", "gender" : "MEN" }, "count" : 1 }
您需要一个额外的 $match
管道步骤,该步骤将根据嵌入字段 "$productAttribute.colour"
存在且不为空来过滤传入的文档:
db.productMetadata.aggregate([
{ $match: {
"productAttribute.colour": {
$exists: true,
$ne: null
}
} },
{ $group: {
_id: {
color: "$productAttribute.colour",
gender: "$productAttribute.gender"
},
count: { $sum: 1 }
} }
]);
也许你应该在 $group 操作之前使用 $match: {'color': {$exists: true}}。使用稀疏索引,它会工作得非常快。
并且根本不要在集合中存储 "null" 字段,这将减少数据库大小并提高 sparse 索引的搜索速度(索引中的文档更少 -> 速度更快)
此示例包含两个不同的集合。为此,我们使用聚合函数。我也在使用 Mongoose
- 我正在使用带有 $lookup
的 customfiellabels 加入 cusmtomfield
- 用 $unwind
展平数组
- $match 以排除文本中包含 INACTIVE 的名称(我使用的是 REGEX)
$project 重命名字段以在客户端上正确显示
。
异步 getAllMasterDataCustomFields(req) {
let response = {};
try {
response = await customfieldsModel.aggregate([
{
$lookup: {
from: 'customfieldlabels',
localField: 'cfId',
foreignField: 'cfId',
as: 'info'
}
},
{ '$unwind': { 'path': '$info', 'preserveNullAndEmptyArrays': true } },
{ '$match': { 'childs.name': { $not: /INACTIVE/ }}},
{
$project: {
'cfId': 1,
'label': '$info.label',
'type': '$info.type',
'childs': 1
}
}]).exec();
} catch (e) {
logger.log('error', `Error while getting response ${e.meesage}`);
}
return response;
}
.
我有 mongo 对文档进行分组操作的查询。
我几乎得到了预期的结果,除了我想在没有空值或空值的情况下优化结果。
目前我的查询是这样的:
db.productMetadata.aggregate([{$group:{"_id":{"color":"$productAttribute.colour","gender":"$productAttribute.gender"},"count" : {$sum : 1}}}]);
结果看起来像这样:
{ "_id" : { "color" : "BLUE", "gender" : "MEN" }, "count" : 1 }
{ "_id" : { }, "count" : 4 }
{ "_id" : { "color" : "NA", "gender" : "WOMEN" }, "count" : 1 }
{ "_id" : { "color" : "BLACK", "gender" : "MEN" }, "count" : 1 }
{ "_id" : { "color" : "BROWN", "gender" : "WOMEN" }, "count" : 1 }
{ "_id" : { "gender" : "MEN" }, "count" : 2 }
{ "_id" : { "color" : "BEIGE", "gender" : "MEN" }, "count" : 1 }
{ "_id" : { "color" : "BROWN", "gender" : "MEN" }, "count" : 1 }
如果在 DB 的实际数据中任何按字段分组的值是空的或 null,我想删除这些行。
例外结果应如下所示:
{ "_id" : { "color" : "BLUE", "gender" : "MEN" }, "count" : 1 }
{ "_id" : { "color" : "NA", "gender" : "WOMEN" }, "count" : 1 }
{ "_id" : { "color" : "BLACK", "gender" : "MEN" }, "count" : 1 }
{ "_id" : { "color" : "BROWN", "gender" : "WOMEN" }, "count" : 1 }
{ "_id" : { "color" : "BEIGE", "gender" : "MEN" }, "count" : 1 }
{ "_id" : { "color" : "BROWN", "gender" : "MEN" }, "count" : 1 }
您需要一个额外的 $match
管道步骤,该步骤将根据嵌入字段 "$productAttribute.colour"
存在且不为空来过滤传入的文档:
db.productMetadata.aggregate([
{ $match: {
"productAttribute.colour": {
$exists: true,
$ne: null
}
} },
{ $group: {
_id: {
color: "$productAttribute.colour",
gender: "$productAttribute.gender"
},
count: { $sum: 1 }
} }
]);
也许你应该在 $group 操作之前使用 $match: {'color': {$exists: true}}。使用稀疏索引,它会工作得非常快。 并且根本不要在集合中存储 "null" 字段,这将减少数据库大小并提高 sparse 索引的搜索速度(索引中的文档更少 -> 速度更快)
此示例包含两个不同的集合。为此,我们使用聚合函数。我也在使用 Mongoose
- 我正在使用带有 $lookup 的 customfiellabels 加入 cusmtomfield
- 用 $unwind 展平数组
- $match 以排除文本中包含 INACTIVE 的名称(我使用的是 REGEX)
$project 重命名字段以在客户端上正确显示
。 异步 getAllMasterDataCustomFields(req) {
let response = {}; try { response = await customfieldsModel.aggregate([ { $lookup: { from: 'customfieldlabels', localField: 'cfId', foreignField: 'cfId', as: 'info' } }, { '$unwind': { 'path': '$info', 'preserveNullAndEmptyArrays': true } }, { '$match': { 'childs.name': { $not: /INACTIVE/ }}}, { $project: { 'cfId': 1, 'label': '$info.label', 'type': '$info.type', 'childs': 1 } }]).exec(); } catch (e) { logger.log('error', `Error while getting response ${e.meesage}`); } return response; }
.