本文共 1620 字,大约阅读时间需要 5 分钟。
在做 MongoDB 的查询操作时,合理使用查询条件符号可以让数据检索更加精准。以下是一些常用的查询条件符号及其使用方法,希望对你有所帮助。
大于 ($gt)
20 的学生: db.getCollection('student').find({'age': {'$gt': 20}}) 大于等于 ($gte)
18 到 25 之间的用户: db.getCollection('user').find({"age": {"$gte": 18, "$lte": 25}}) 小于 ($lt)
20 的学生: db.getCollection('student').find({'age': {'$lt': 20}}) 小于等于 ($lte)
15 到 20 之间的学生: db.getCollection('student').find({'age': {'$lte': 20}}) 不等于 ($ne)
20 的学生: db.getCollection('student').find({'age': {'$ne': 20}}) 在范围内 ($in)
20 到 23 之间的学生: db.getCollection('student').find({'age': {'$in': [20, 23]}}) 不在范围内 ($nin)
20 到 23 之间的学生: db.getCollection('student').find({'age': {'$nin': [20, 23]}}) 正则匹配 ($regex)
M 开头的用户: db.getCollection('user').find({ 'name': { '$regex': '^M.*' }}) 属性是否存在 ($exists)
name 字段的用户: db.getCollection('user').find({'name': {'$exists': true}}) 类型判断 ($type)
db.getCollection('user').find({'age': {'$type': 'int'}}) 文本查询 ($text)
Mike 的文本内容: db.getCollection('user').find({ '$text': { '$search': 'Mike' }}) 或条件 ($or)
name 为 chen 或 wang 的用户: db.getCollection('user').find({ '$or': [ {'name': 'chen'}, {'name': 'wang'} ]}) 在处理日期查询时,可以使用以下方式:
db.getCollection('news').find({ 'pub_date': { '$gte': '2017-07-11 11:00:00' }}) 希望以上内容对你有所帮助,记得在实际使用前根据需要调整查询条件符号和具体值。
转载地址:http://bjffk.baihongyu.com/