我无法在聚合中使用以下正则表达式。我读了很多例子,但它没有用。我想要的是排除具有3个或更少字符的单词(例如:1 a2 aaa a-)。然而,他们仍然出现。
这是我的正则表达式(我认为这是正确的)\b\w?\w?\w?\b
。
这是聚合:
Meteor.publish("topics", function () {
ReactiveAggregate(this, Articles, [
{
$project: {
topic: { $split: ["$title", " "] },
article: "$$ROOT"
}
},
{
$unwind: {
path: "$topic"
}
},
{
$match: {
topic: { $nin: ["sports", "\b\w?\w?\w?\b"] }
}
},
{
$group: {
_id: "$topic",
count: { $sum: 1 },
articles: { $push: "$article" }
}
}
], { clientCollection: "clientTopic" });
});
示例:
/* Article 1 */
{
"_id" : "SB4mKAxaBijQXnS73",
"title" : "Messi signs new contract with Barcelona"
}
/* Article 2 */
{
"_id" : "rhqioBkePzGCrRFLp",
"title" : "Messi has 30 years old"
}
/* Article 3 */
{
"_id" : "X6LochRZw32op39W8",
"title" : "President of Argentina visits Messi"
}
我想得到一个像:
这样的集合Messi ==> {_id: "Messi", articles: [Article 1, Article 2, Article 3], count: 3 }
signs ==> {_id: "signs", articles: [Article 1], count: 1 }
contract ==> {_id: "contract", articles: [Article 1], count: 1 }
with ==> {_id: "with", articles: [Article 1], count: 1 }
Barcelona ==> {_id: "Barcelona", articles: [Article 1], count: 1 }
years ==> {_id: "years", articles: [Article 2], count: 1 }
President ==> {_id: "President", articles: [Article 3], count: 1 }
Argentina ==> {_id: "Argentina", articles: [Article 3], count: 1 }
visits ==> {_id: "visits", articles: [Article 3], count: 1 }
如您所见,单词new
,has
,30
,old
,of
不应出现,因为它们的长度不超过3个字符
其标题中包含sports
字样的文章不包含在集合中,但是与正则表达式匹配的文章包含。
答案 0 :(得分:0)
数组中的每个元素在单词前面用^读取,例如[' ^工作',' ^ accus',' ^ planet']
OR
var words = [/ hello /,/ ^ world /]; //正则表达式数组 db.getCollection(' word collection')。find({" word":{$ in:words}});
答案 1 :(得分:0)
如果要排除长度为3或小于3的单词,则可以使用/ \ w {4,} /
Meteor.publish("topics", function () {
ReactiveAggregate(this, Articles, [
{
$project: {
topic: { $split: ["$title", " "] },
article: "$$ROOT"
}
},
{
$unwind: {
path: "$topic"
}
},
{
$match: {
topic: { $nin: ["sports", "\w{4,}"] }
}
},
{
$group: {
_id: "$topic",
count: { $sum: 1 },
articles: { $push: "$article" }
}
}
], { clientCollection: "clientTopic" });
});
您可以将此链接用于测试正则表达式https://regex101.com/