管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
查询表中学生年级大于20,如下:& d! R! L8 Q! Z* K4 \0 j
- t# p& B+ U% A5 p+ L! b
db.getCollection('student').find({'age':{'$gt':'20'}})
9 Y0 }6 Z9 y4 Q
$ v7 J/ p0 ^. |6 z& S x Z# s* R4 U6 m/ O
0 i0 h+ C. @ X2 z2 P$lt < (less than )7 D( B- b. h) r8 e4 v
. a, u% a4 v$ H+ u# [
$lte <= (less than or equal to )% M x* G) @! ^/ a% K
. m1 ^6 @9 q6 H+ S" {
$gt > (greater than )' k% R. Z+ r6 D, ~( r! j' W
3 o7 U0 C7 G T2 r, X# g% d9 X$gte >= (greater than or equal to)
* w6 L8 }) H* @5 x5 l# A; _5 c. |2 e: N( U. |! R- X
/ h$ f# V3 m. x5 j$ Z1 \
: c' K; D' N. I$ne != (not equal to)不等于 {'age': {'$ne': 20}}: v0 D1 r% z1 U1 t+ T5 I8 v( \
9 c: I9 b9 @, r) w7 M' `3 v5 D$in 在范围内 {'age': {'$in': [20, 23]}} 注意用list) N9 r( n3 W% C
/ f" m% y: q' k5 N, H; ^6 k+ T$nin (not in) 不在范围内{'age': {'$nin': [20, 23]}} 注意用list
2 |5 o/ G" J4 @) @9 E9 J/ ^8 {1 H, z6 j
$regex (正则匹配) db.collection.find({'name': {'$regex': '^M.*'}}) 匹配以M开头的名字/ k. Z- f0 l) C. b
. K0 y) X: n8 q+ }: c
$exists 属性是否存在 {'name': {'$exists': True}} 查找name属性存在
% n4 r- @- \9 [6 _- P
% @9 J9 b \0 |7 q4 r$type 类型判断 {'age': {'$type': 'int'}} age的类型为int- S2 ?. y, |2 x
# r3 r: X# y" n$text 文本查询 {'$text': {'$search': 'Mike'}} text类型的属性中包含Mike字符串& h+ i/ M+ l# ]1 E5 h9 Y! K6 Y6 B8 s4 Y
" ?" e K) ` N6 [3 F, i
$or 查找多种条件 ({'$or':[{'name':'chen'},{'name':'wang'}]})! S6 g5 g' \+ t1 V
$where 查询两个键的值是否相等 db.test.find({"$where": "this.fields1 == this.fields2"}).limit(10);
" R; W0 s+ `4 U1 S( }: N: W. ~/ C$ \: R: @# L' [. O( z
$exists 键是否存在 {finaGoalLine:{'$exists':true}}3 n- M$ M2 J; T9 B% q* f
% L1 W. z! s" \. w
$all 如果您希望找到一个包含这两个元素的数组 [ 'tags' => [ '$all' => [ 'red' , 'blank' ]]]
+ y* h* ?' y+ T, Q) V$ E
5 d$ n7 g5 [% \' o+ g$elemMatch 运算符在数组的元素上指定多个条件 'dim_cm' => ['$elemMatch' => [ '$gt' => 22,'$lt' => 30,],],
6 C0 @: _$ {# [4 {" S$size 运算符按元素数查询数组 [ 'tags' => [ '$size' => 3
( O0 S5 a3 u" e, J ?& U% h/ \) q5 _7 y+ L. z& J
7 l# k! i5 G: S* K1 W& z
4 z. W* W/ P0 V/ t& K7 X0 y( ~( `+ Q# y6 k7 I% N) x+ h6 {) r
1 {* B. j) ^8 [! ]$ F, s9 d9 U$ @) O
# S" i9 u8 T& e* z# i3 D
a5 S' S& z; Y) Z; U; x i7 p A组合使用方法如下:
, S5 t% g, }7 N
, e7 w& r* I# bdb.user.find({"age":{"$gte":18,"$lte":25}})4 [( o; R" W8 {" N( m
! `) B+ r( b, g( b
! J- n! L; c( B0 g/ K
8 J+ n4 O7 k, V: @* l y }
对于日期的条件查询方法:
2 w( A5 C& M) _, J1 J/ b) @: M* `& `3 c, c+ A3 ]; z4 u
db.getCollection('news').find({'pub_date':{'$gte':'2017-07-11 11:0:0'}})
4 X7 v& A. c \
5 C5 X% U) B9 x: y6 I/ T* \
% @6 t! {" b: Z8 l1 [+ K* L, }, ^/ ~: C. T8 i" R
2) 不等于 $ne+ l e( D' y- J
# A2 n9 V+ `# d6 ^7 I' @3 }
例子:
# X$ n# i/ F) M! d5 z. J5 }# Y: |. @- `. ~
db.taobao.find( { age: { $ne : 10} } );- o. S% w0 @) o, a$ |* e: w
php7 查询mongodb方法大全7 ~# Q& x3 d! G% P n. R
* Y( Z$ S" g2 M6 ~$ n, K
% ?8 m" q2 L( |/ q9 r3 a6 G- G1 B& _, x, O
+ G5 P0 g+ x/ K1 X, J! u
0 w S7 L2 `: F P/ X8 Y
: {) Z& o3 A6 D, K7 \) ]+ w5 W" r, M# u, ^
' _( }) M3 ~7 _8 v
8 z# ?5 X$ W- s1 Y9 }# Z F2 ~
# c! i# w0 G% @; G. a9 W" P* T* t" o( v( G' \* x8 x" _: {; A* k
8 Q' c/ e' W) o& v |
|