运算符
运算符一般用于连接运算数或参数等单个数据项并返回结果。从语法上讲,运算符出现在操作数之前、操作数之后或两个操作数之间均可。
运算符示例
数据筛选(where)
等于
使用 $eq 表示等于,写法如下:
where={"category": {"$eq": "AI"}}
不等于
使用 $ne 表示不等于,写法如下:
where={"status": {"$ne": "deleted"}}
大于
使用 $gt 表示大于,写法如下:
where={"score": {"$gt": 90}}
大于或等于
使用 $gte 表示大于或等于,写法如下:
where={"score": {"$gte": 90}}
小于
使用 $lt 表示小于,写法如下:
where={"score": {"$lt": 50}}
小于或等于
使用 $lte 表示小于或等于,写法如下:
where={"score": {"$lte": 50}}
包含
使用 $in 表示包含,写法如下:
where={"tag": {"$in": ["ml", "python", "ai"]}}
不包含
使用 $nin 表示不包含,写法如下:
where={"tag": {"$nin": ["deprecated", "old"]}}
逻辑或
使用 $or 表示逻辑或,写法如下:
where={
"$or": [
{"category": {"$eq": "AI"}},
{"tag": {"$eq": "python"}}
]
}
逻辑与
使用 $and 表示逻辑与,写法如下:
where={
"$and": [
{"category": {"$eq": "AI"}},
{"score": {"$gte": 90}}
]
}
文本筛选(where_document)
全文搜索(包含子字符串)
使用 $contains 表示全文搜索,写法如下:
where_document={"$contains": "machine learning"}
正则表达式
使用 $regex 表示正则表达式,写法如下:
where_document={"$regex": "pattern.*"}
逻辑或
使用 $or 表示逻辑或,写法如下:
where_document={
"$or": [
{"$contains": "machine learning"},
{"$contains": "artificial intelligence"}
]
}
逻辑与
使用 $and 表示逻辑与,写法如下:
where_document={
"$and": [
{"$contains": "machine"},
{"$contains": "learning"}
]
}