在表达式下匹配多个条件 [英] Match multiple conditions in an aggregate under expressions
本文介绍了在表达式下匹配多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
文档的样本格式:
{
"_id": {
"$oid": "5e158e2de6facf7181cc368f"
},
"word": "as luck would have it",
}
我正在尝试将表达式中的多个条件匹配为:
I am trying to match multiple conditions in an expression as:
query = {
"$match": {
"$expr": {"$eq": [{"$strLenCP": "$word"}, 6],
'$lt': [
{
'$size': {
'$split': [
"$word",
" "
]
}
},
2
]
}
}
}
和管道如下:
pipeline = [query]
cursor_objects = db['test'].aggregate(pipeline)
在上面的查询中,我试图实现单词长度必须为6并且不包含空格
In the above query I am trying to achieve word length must be 6 and it doesn't contain any spaces
当我这样做时,我会遇到错误:
When I did this I am getting and error :
pymongo.errors.OperationFailure: An object representing an expression must have exactly one field: { $eq: [ { $strLenCP: "$word" }, 6 ], $lt: [ { $size: { $split: [ "$word", " " ]
我可以知道如何实现吗?
May I know how could I achieve this ?
感谢任何帮助,... TIA
any help is appreciated ,...TIA
推荐答案
To use multiple conditions in $expr
you have to use $and
operator
query = {
$match: {
$expr: {
$and: [
{
$lt: [
{
$size: {
$split: ["$word", " "]
}
},
2
]
},
{ $eq: [{ $strLenCP: "$word" }, 6] }
]
}
}
};
这篇关于在表达式下匹配多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文