管理员
![Rank: 64](template/yeei_dream1/css/yeei//star_level3.gif) ![Rank: 64](template/yeei_dream1/css/yeei//star_level3.gif) ![Rank: 64](template/yeei_dream1/css/yeei//star_level3.gif) ![Rank: 64](template/yeei_dream1/css/yeei//star_level3.gif)
论坛积分
分
威望 点
贡献值 个
金币 枚
|
查询表中学生年级大于20,如下:$ y" a' Z2 ^; i& |) o8 K
! u( v o' {9 |; }7 o& J
db.getCollection('student').find({'age':{'$gt':'20'}})- A! ?& V5 L; H6 I7 |
2 g9 R3 P! W- C+ V
+ }; c4 t7 [3 P4 Y
+ s6 _5 i2 W6 r/ x# ^$ f$lt < (less than )
! c0 ^* c! k- s$ l, v' u
7 I! v0 c. u7 f. z& `8 p$lte <= (less than or equal to )7 V, x2 u; X1 Z# q1 x3 r' z
7 P1 ^9 y3 \7 N5 b
$gt > (greater than )
3 N1 {% i2 J I5 v8 d. w5 v! P" t' d _" Y/ c, E3 R' t6 _
$gte >= (greater than or equal to)% h' S& |* y( E, }: k" N+ s* ~9 q; v
2 N: k# ^# J3 ]4 j5 M: c6 x. z: c, e L* I( c; [; v( Y+ C
7 Y0 F5 b) Y; S
$ne != (not equal to)不等于 {'age': {'$ne': 20}}
9 M5 x2 U4 A# W) I+ O' M
5 O I! ^9 [" d5 P. ~$in 在范围内 {'age': {'$in': [20, 23]}} 注意用list
1 F6 o& _, ~6 ?3 Y# S
8 r! f; J7 z/ T4 |0 R; u$nin (not in) 不在范围内{'age': {'$nin': [20, 23]}} 注意用list
E( e; ?( H2 R, e0 Q- z- K; x! O" Z ~
$regex (正则匹配) db.collection.find({'name': {'$regex': '^M.*'}}) 匹配以M开头的名字
* W W: g0 r: v7 F
s1 d4 d& N7 K' d8 @$exists 属性是否存在 {'name': {'$exists': True}} 查找name属性存在! y3 D6 q5 g9 k- e. d, h
* V5 \' Q. Q/ R
$type 类型判断 {'age': {'$type': 'int'}} age的类型为int3 A1 y; T' a( W/ U% S, z
6 g0 ~8 X7 K$ C9 b& Y
$text 文本查询 {'$text': {'$search': 'Mike'}} text类型的属性中包含Mike字符串
7 m) K u/ C6 N1 C
6 M. a0 U# N C# a& w* r: {$or 查找多种条件 ({'$or':[{'name':'chen'},{'name':'wang'}]})6 Y; ~% R' P! H, S7 n( y- F1 x
$where 查询两个键的值是否相等 db.test.find({"$where": "this.fields1 == this.fields2"}).limit(10);
1 J: G$ O6 O K7 e6 z: n( b$ e: g8 Z6 \# a Q* Y
$exists 键是否存在 {finaGoalLine:{'$exists':true}}
! x! _9 [/ R% S) |- m( W0 x E" V4 I- s- T
$all 如果您希望找到一个包含这两个元素的数组 [ 'tags' => [ '$all' => [ 'red' , 'blank' ]]]2 K( I/ r4 `+ ^4 w0 d4 ?0 p# {& F: U
: y. ^# ]! ]: Q+ H
$elemMatch 运算符在数组的元素上指定多个条件 'dim_cm' => ['$elemMatch' => [ '$gt' => 22,'$lt' => 30,],],
7 z( k' Z. e4 M, n6 E$size 运算符按元素数查询数组 [ 'tags' => [ '$size' => 3
2 j8 V6 X5 m* G6 N, g# ?" S
; D6 ]$ O& }! F0 A/ n( C1 s& J" z& u: _ g* {. \4 X/ b$ }( e3 r
, U3 K9 K4 I: E1 m" D2 S
% m* } \+ ?3 N4 N) T7 \
1 Q6 ]' j% K6 L, Q+ x, }7 b3 B; ? P# ~0 p* X1 i7 @
9 h d) {7 D6 R2 D) Y
5 T* S/ {' J. l
组合使用方法如下:5 S" \) u. K/ g
, x1 v3 A( Y7 U; W. ndb.user.find({"age":{"$gte":18,"$lte":25}})9 F7 X0 Z% `; C9 I
: v% U# _7 A, A9 P8 F% t
, L5 w) S2 j& Y7 L/ J& r
$ p* x/ S* w E b对于日期的条件查询方法:# {- ?$ h. F/ d& v& P W
, Y0 m, q% U& @- mdb.getCollection('news').find({'pub_date':{'$gte':'2017-07-11 11:0:0'}})9 i+ e/ {; v, o6 r6 h2 E
- n5 \+ @3 U, ~
7 i/ d/ t2 E$ q7 B* G' C; P0 k
* Y( |4 p2 Y& P- Q7 \9 j2) 不等于 $ne
9 s S: W8 B2 y& G- z9 n! K
5 |& D, Y$ a! ~7 H( p+ t例子:
9 S! m" r. A& [
# B3 ^0 M: I" @" ndb.taobao.find( { age: { $ne : 10} } );
5 g9 H) l ]; W& o0 P. sphp7 查询mongodb方法大全
4 l6 o) p3 }; P! D. `% {$ l% u5 ~: ^! v4 W1 j: Q9 E9 q9 T
/ l1 o: t# N* A0 k! @- y& M) F z# U( U3 F; {& F+ ~
6 ^) x, b1 U0 h. A
- t W1 O" a; H% W; {# z! \+ I
3 H) X! I2 K7 _
" Q1 Z, ^) H5 K. w; P0 B m4 r. N4 t2 |4 q3 s/ H. x9 {
/ s' n" C5 z2 U* N& p
# H$ h6 y1 ]" e+ t5 f) |$ V2 G
% c% u V" w2 Q6 D) D' U0 Q$ E! h% P6 q) E# _, |0 P3 U+ e& I
|
|