我知道如何进行不区分大小写的搜索。
示例:
aaa@aaa.com
但: users.email:AAA@aaa.com
db.getCollection('members').aggregate([
{$lookup: {'from': 'users', 'localField': 'members.email', 'foreignField': 'email', 'as': 'users'}},
{$unwind: {path: '$users', preserveNullAndEmptyArrays: true}},
{$match : { users: { $exists: false } }},
]);
集合成员如下:
{
"_id" : ObjectId("5b439c02a5439e00568291fe"),
"members" : [
{
"email" : "aaa@gmail.com"
},
{
"email" : "bbb@gmail.com"
},
{
"email" : "ccc@gmail.com"
}
],
"groupname" : "abuse",
"active" : true,
"createdAt" : ISODate("2018-07-09T17:31:46.098Z"),
"updatedAt" : ISODate("2018-07-09T17:31:46.098Z"),
"__v" : 0
}
集合member_user如下:
{
"_id" : ObjectId("5a7dfce545e13f0233b2e451"),
"email" : "AAA@gmail.com",
"__v" : 0
}
{
"_id" : ObjectId("5b43ac89bfd90aa9cc4a68c2"),
"email" : "bbb@gmail.com",
"__v" : 0
}
这是我使用的代码:
db.getCollection('member').aggregate([
{$unwind: "$members"},
{$lookup: {'from': 'member_user', 'localField': 'members.email', 'foreignField': 'email', 'as': 'users'}},
{$unwind: {path: '$users', preserveNullAndEmptyArrays: true}},
{$match : { users: { $exists: false } }},
],
{
collation: {
locale: 'en_US',
strength: 1
}
}
);
当数据集较小时,速度始终非常快。但是我有24K成员和7K用户。就像我说的那样,当{$ match:{用户:{$ exists:true}}}时,速度还可以,但是当{$ match:{用户:{$ exists:false}}}}时,速度将近60倍。 (1秒1分钟)
没有排序规则,速度总是很快的
答案 0 :(得分:0)
您需要使用$toLower
聚合将foreignField
和db.collection.aggregate([
{ "$unwind": "$members" },
{ "$addFields": { "members.email": { "$toLower": "$members.email" }}},
{ "$lookup": {
"from": "users",
"let": { "membersEmail": "$members.email" },
"pipeline": [
{ "$addFields": { "email": { "$toLower": "$email" }}},
{ "$match": { "$expr": { "$eq": [ "$email", "$$membersEmail" ] } } }
],
"as": "members.email"
}},
{ "$unwind": "$members.email" },
{ "$group": {
"_id": "$_id",
"groupname": { "$first": "$groupname" },
"active": { "$first": "$active" },
"members": { "$push": "$members.email" }
}}
])
转换为小写,然后才能轻松匹配字段。
sudo port upgrade qt59-qtdoc