管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
查询表中学生年级大于20,如下:; ]5 S. F t; c6 h0 e3 @
( |. l9 m/ ^' Z6 ]
db.getCollection('student').find({'age':{'$gt':'20'}})
& w. e; M% J' K$ X0 T+ g0 [
/ Z+ o: u A' i5 j+ ~/ s! n& x) I* b2 e0 Q/ D$ M
/ O) s% N" @) k# w& U# Q6 |
$lt < (less than )
& R5 V9 O x% B* f% m2 |' m/ R* {0 g1 G6 M5 e T& E+ W5 I
$lte <= (less than or equal to )
5 z% h" ~9 X, z. d& {9 l4 H" W! {8 L y* t- E( U
$gt > (greater than )
+ s- u% A' P9 A! Z8 i2 u9 L( y8 O) A1 Q" j6 f; b
$gte >= (greater than or equal to)/ s8 g( l$ L7 B
* }5 I9 O+ ]: u0 Z2 [% e
4 l$ `; V$ D# ^0 x+ D: E8 T) W
7 ^, ]5 T- V$ I$ R7 l( }, a$ne != (not equal to)不等于 {'age': {'$ne': 20}}
4 V9 v2 a* k+ p9 _
) b: Y* S N$ j) G/ b* K* n% L$in 在范围内 {'age': {'$in': [20, 23]}} 注意用list
9 @7 B& i, r7 a2 q- V% M. X
$ S2 X& M f: ]1 j: }. L' E/ x1 }$nin (not in) 不在范围内{'age': {'$nin': [20, 23]}} 注意用list% T i, c+ V. _9 _0 ]/ }1 u
* M5 P1 t! d# d- w7 D% k$regex (正则匹配) db.collection.find({'name': {'$regex': '^M.*'}}) 匹配以M开头的名字
$ _, o$ r& p+ _8 r. Q7 p7 @! j" C# i# h" B. F
$exists 属性是否存在 {'name': {'$exists': True}} 查找name属性存在/ R$ |6 Q: f( Z2 h- }* J
H* I R q( `/ T$type 类型判断 {'age': {'$type': 'int'}} age的类型为int7 [/ U* ]1 m1 S5 V0 K) u& u
& z4 s3 x) f9 I" m5 v0 y) s
$text 文本查询 {'$text': {'$search': 'Mike'}} text类型的属性中包含Mike字符串* m) `9 r0 T: b' K: Q x/ L, t
) R2 K" l, [/ }6 S# P' V1 [, ~6 |
$or 查找多种条件 ({'$or':[{'name':'chen'},{'name':'wang'}]})7 i1 u' k r0 K) v' \0 M6 J% X$ I z
$where 查询两个键的值是否相等 db.test.find({"$where": "this.fields1 == this.fields2"}).limit(10);" F2 L! r3 E4 q- k
E( P, I! ?* W3 U+ s$exists 键是否存在 {finaGoalLine:{'$exists':true}}
' K3 ~* m1 c, A' `6 o1 M
2 G9 _- y& F% l$all 如果您希望找到一个包含这两个元素的数组 [ 'tags' => [ '$all' => [ 'red' , 'blank' ]]]
* c3 W3 D7 } y, ~8 s3 k, {+ N! G8 P! P C T u* ?0 j) D% k
$elemMatch 运算符在数组的元素上指定多个条件 'dim_cm' => ['$elemMatch' => [ '$gt' => 22,'$lt' => 30,],],( j$ c. \+ r% E, N
$size 运算符按元素数查询数组 [ 'tags' => [ '$size' => 3 $ A) G) L% F: [5 e% P" }' r0 C* L
) m( J% o' q. D$ W6 w* g6 K
$ U5 L# B8 e1 y+ d! z
u8 ?& U9 s5 ~- m; p; W1 _: k4 ?" t7 V. n
6 _+ x9 o. Q& |: ^' _- Z1 v
) h4 a# F# B- W" B
' Z% b5 w$ P# L! u2 e* P7 j
1 A |! z, M4 i9 V组合使用方法如下:6 S3 D1 h# T0 F; g, ?; `1 r8 E+ x2 U
3 ^: l* m) B% Z& @/ H
db.user.find({"age":{"$gte":18,"$lte":25}})
, Y" G R+ |0 T: ~! U7 U# }* e3 e$ J
% ]! c( O' Z3 i# ]+ w* `$ ~( T! q* K6 R. f
对于日期的条件查询方法:
5 p- ?1 Q5 R. Y9 L! b q% c
# y: w6 S/ n% k' b& x3 ddb.getCollection('news').find({'pub_date':{'$gte':'2017-07-11 11:0:0'}}). P" _! w1 N. W+ v0 c! s
2 {$ @! Q4 V% F4 T7 `; ~1 Q
. V7 D- ?. d y7 ^; a
" E( V5 s: T8 t% i0 t2) 不等于 $ne, t/ Z- [: v! ~2 w% A7 h
: D+ [0 R) }4 t
例子:
" j0 I- h+ ?# i7 S! n% G* k& F( n+ V0 _# |4 j; P( q( e$ v! @; B0 K7 A
db.taobao.find( { age: { $ne : 10} } );+ q0 I! R- t, I A
php7 查询mongodb方法大全
( h* K9 i& h9 W2 T# P
2 W& |7 Y4 G$ e* y4 G1 y& f6 `9 h8 M8 Y n
3 r+ G# p5 F6 P' K, _/ x8 @. P, X9 t5 H& |: g
5 o" G: u! V% {7 s9 ? \3 ?
$ }+ z7 r; I- }
u% @( Y/ X C
# U2 |8 I- Y7 i1 |2 {2 |
% g% l3 ~: U% [
* V. j: R) G' U+ o$ c
# Z8 f% e' G2 ^+ Y1 F
4 y9 v* f4 j" M& a) y0 m% Z5 L9 p+ O, N" H3 b* C: T
|
|