查詢表中學生年級大於20,如下:
db.getCollection('student').find({'age':{'$gt':'20'}})
$lt < (less than )
$lte <= (less than or equal to )
$gt > (greater than )
$gte >= (greater than or equal to)
$ne != (not equal to)不等於 {'age': {'$ne': 20}}
$in 在范圍內 {'age': {'$in': [20, 23]}} 注意用list
$nin (not in) 不在范圍內{'age': {'$nin': [20, 23]}} 注意用list
$regex (正則匹配) db.
collection.find({
'name'
: {
'$regex'
:
'^M.*'
}}) 匹配以M開頭的名字
$exists 屬性是否存在 {'name': {'$exists': True}} 查找name屬性存在
$type 類型判斷 {'age': {'$type': 'int'}} age的類型為int
$text 文本查詢 {'$text': {'$search': 'Mike'}} text類型的屬性中包含Mike字符串
$or 查找多種條件 ({'$or':[{'name':'chen'},{'name':'wang'}]})
組合使用方法如下:
db.user.find({"age":{"$gte":18,"$lte":25}})
對於日期的條件查詢方法:
db.getCollection('news').find({'pub_date':{'$gte':'2017-07-11 11:0:0'}})