运算符
运算符一般用于连接运算数或参数等单个数据项并返回结果。从语法上讲,运算符出现在操作数之前、操作数之后或两个操作数之间均可。
运算符示例
数据筛选(where)
等于
使用 $eq 表示等于,写法如下:
where: { category: { $eq: "AI" } }
也可以使用简化形式,写法如下:
where: { category: "AI" }
不等于
使用 $ne 表示不等于,写法如下:
where: { category: { $ne: "ML" } }
大于
使用 $gt 表示大于,写法如下:
where: { score: { $gt: 80 } }
大于或等于
使用 $gte 表示大于或等于,写法如下:
where: { score: { $gte: 90 } }
小于
使用 $lt 表示小于,写法如下:
where: { score: { $lt: 60 } }
小于或等于
使用 $lte 表示小于或等于,写法如下:
where: { score: { $lte: 50 } }
包含
使用 $in 表示包含,写法如下:
where: { tag: { $in: ["ml", "python", "ai"] } }
不包含
使用 $nin 表示不包含,写法如下:
where: { tag: { $nin: ["deprecated", "obsolete"] } }
逻辑或
使用 $or 表示逻辑或,写法如下:
where: {
$or: [
{ category: "AI" },
{ category: "ML" }
]
}
逻辑与
使用 $and 表示逻辑与,写法如下:
where: {
$and: [
{ category: { $eq: "AI" } },
{ score: { $gte: 90 } }
]
}
文本筛选(where_document)
全文搜索(包含子字符串)
使用 $contains 表示全文搜索,写法如下:
whereDocument: { $contains: "机器学习" }
正则表达式
使用 $regex 表示正则表达式,写法如下:
whereDocument: { $regex: "AI|ML|DL" }
逻辑或
使用 $or 表示逻辑或,写法如下:
whereDocument: {
$or: [
{ $contains: "AI" },
{ $contains: "人工智能" }
]
}
逻辑与
使用 $and 表示逻辑与,写法如下:
whereDocument: {
$and: [
{ $contains: "机器学习" },
{ $contains: "深度学习" }
]
}