管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
查询表中学生年级大于20,如下:+ ^ q5 `7 Q) c- g1 ^* s8 P9 H
7 d2 S2 m3 K2 u+ @3 f, Edb.getCollection('student').find({'age':{'$gt':'20'}})6 C0 s( f8 `4 _" Y3 H
+ w$ ]' j$ B( M4 V! y# D' [, `
8 M" P7 |# \( Y& Z6 d* J9 v
$lt < (less than )2 h( L4 K- S- U2 e+ Z0 R3 @/ _
1 U" v8 U) O0 c& d5 V
$lte <= (less than or equal to )
A& W- {: J+ e3 n9 W. ]+ \' `3 P/ o- ~! n; O! Q; E- K
$gt > (greater than )6 T$ @$ g8 }0 s
6 N9 t5 ^9 ^) V0 s+ W& m& C2 ?" }
$gte >= (greater than or equal to)7 v! i! { {: J2 }8 p6 _& P% z4 `
* h* s* l3 L) I; x0 y1 s
% ]; m6 d) k1 S$ u7 r8 c5 l' ^5 d6 A5 K& H
$ne != (not equal to)不等于 {'age': {'$ne': 20}}/ e7 Z: M+ O3 B* f1 X7 `
/ `( F( H) l8 |4 S/ W$in 在范围内 {'age': {'$in': [20, 23]}} 注意用list
6 w0 y6 [# r) H1 i3 d/ c; a8 c+ k2 F# B# I2 c
$nin (not in) 不在范围内{'age': {'$nin': [20, 23]}} 注意用list
0 B, n' c6 L/ x9 `; ~
/ F8 t6 D j a$regex (正则匹配) db.collection.find({'name': {'$regex': '^M.*'}}) 匹配以M开头的名字
2 y/ N1 o& Y2 Z& c I, a) t; K# h ^
K/ G* b' B1 B: V- \( g; i0 j$exists 属性是否存在 {'name': {'$exists': True}} 查找name属性存在
4 v/ X2 P8 s9 Z) D6 q( [' }* @% O8 w+ I, ?
$type 类型判断 {'age': {'$type': 'int'}} age的类型为int8 m' W7 |5 n2 I: o( p9 D4 a
: k( x: ?6 I* T) Y5 a, U5 R1 s
$text 文本查询 {'$text': {'$search': 'Mike'}} text类型的属性中包含Mike字符串. \! E' i( K7 Y
. o% o H$ z. X) v- O! U) B$or 查找多种条件 ({'$or':[{'name':'chen'},{'name':'wang'}]})
6 o6 n* ~5 }: P& k8 g/ W9 D$where 查询两个键的值是否相等 db.test.find({"$where": "this.fields1 == this.fields2"}).limit(10);3 h& e9 R7 z/ `, D" O+ _3 M
& R" `( l7 ?% x1 w
$exists 键是否存在 {finaGoalLine:{'$exists':true}}* T6 }+ k, x# G- ]
! F& C3 L( H o7 T6 d
$all 如果您希望找到一个包含这两个元素的数组 [ 'tags' => [ '$all' => [ 'red' , 'blank' ]]]
4 `$ J% e1 V3 `0 I0 U0 A1 m( ^ L$ h) l$ _, r
$elemMatch 运算符在数组的元素上指定多个条件 'dim_cm' => ['$elemMatch' => [ '$gt' => 22,'$lt' => 30,],],
1 ?- l9 t0 x5 C. b" ?$size 运算符按元素数查询数组 [ 'tags' => [ '$size' => 3
3 j! D3 u- q; w* }9 e1 s- Z+ ~% V9 N5 S& Z" X+ }
& q' f3 b* u' a
v( K. h+ n! t0 n; @
* s1 e2 [' N* n8 {& H" W
) }0 Z( B8 U+ G) u2 X6 g9 Q4 F$ `2 z+ u, t: H
" R+ m; N5 G$ f- @* q4 Z' `1 }' n5 G7 y: d M
组合使用方法如下:# u5 c& P* M5 [" {8 J8 f2 n
9 p0 J; }! {' ]* A" b
db.user.find({"age":{"$gte":18,"$lte":25}})
0 G3 W0 q: y/ l; d J/ U* ~
, L5 a4 Y: b. a8 T- e4 B
. I% z$ o3 e1 Z# m0 L6 }9 E! r) E# _' _6 l& \
对于日期的条件查询方法:( T H) b, j4 T' v
& {) I( K; d4 H/ ?2 g. H
db.getCollection('news').find({'pub_date':{'$gte':'2017-07-11 11:0:0'}})5 M. U1 {, U" E* T
* v9 K. b. G X
4 P3 j8 U8 L+ T
* F" }) n( m, K1 o8 Y2) 不等于 $ne
& u# a; R. l3 W/ C, |! Y5 v6 [6 j) }* h! Y* f1 L
例子:% M' D, C' @; _) j9 _- W
5 O; X$ o: V. f( ]% Udb.taobao.find( { age: { $ne : 10} } );- Y$ F% k g5 M& z3 l z; o. ]4 f
php7 查询mongodb方法大全
9 x0 q. m% [ d9 M; u9 [) E* Q4 I% m
& N0 j, X" _. O, `* v6 V
8 R# R3 q" k n0 T, a4 m7 l* A& e5 {6 @5 }6 q
# G) _4 Q$ d* Q" i: g2 u3 q
$ Q$ ^, J+ w4 \7 G$ h9 s" M
0 M3 c& b |+ Q, T$ f, i7 [
/ \; N3 r. m9 g' a: H& P6 E" W' j3 P9 w$ }' {4 T: z' s: i) U
' B, ]$ C4 Z! v9 i: \0 j! I1 y
+ F( ?% ^; z# r
7 W1 G8 X& [* t; d- V' { |
|