管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
查询表中学生年级大于20,如下:
$ c3 V' e2 W; M/ O8 j
2 }; W) z! @* R4 D& E! Edb.getCollection('student').find({'age':{'$gt':'20'}})
( C1 f& s. ^ C. D2 ~$ i" V
9 g! P7 L& k1 G
0 s' D- O; I7 L9 ?& _' w5 S: y" _/ x/ ?, J% R
$lt < (less than ): | y/ t- b/ ?# b u# Q" i
% E+ e) g' R: F
$lte <= (less than or equal to )
: R/ s o! ~+ A! m7 e; S, q$ s2 M9 L" L% q9 q
$gt > (greater than )2 v* N$ u) O) O5 ~
1 }2 k5 N. x% y/ J" N3 d5 _
$gte >= (greater than or equal to)
. c. b- z$ v3 |6 x+ B1 k8 C! i# L+ d: u
# d6 p+ B6 N1 f/ o3 I3 }, |- Q4 X$ _: L! J2 [
$ne != (not equal to)不等于 {'age': {'$ne': 20}}
% p, B. `3 H5 o- c5 w
2 ~. a, D( Y- m2 q' O$ A9 r$in 在范围内 {'age': {'$in': [20, 23]}} 注意用list8 g" t) X8 P B" D
! ~& I- c0 ^! u4 h9 G# j
$nin (not in) 不在范围内{'age': {'$nin': [20, 23]}} 注意用list4 V1 a" U: h/ ~4 a
, ]- [* L3 C9 U
$regex (正则匹配) db.collection.find({'name': {'$regex': '^M.*'}}) 匹配以M开头的名字- d3 T* r7 {: M$ }5 Y
% w* P/ T1 d0 c+ _5 ]
$exists 属性是否存在 {'name': {'$exists': True}} 查找name属性存在
- k. o1 t; R, ?/ c6 F3 X0 N( ~
( h" @( c) p; }' f0 h7 d9 n) W$ ]$type 类型判断 {'age': {'$type': 'int'}} age的类型为int
3 b" T5 `$ ?) L9 O8 l+ [0 C
$ O: s* E1 S% D2 C/ J$text 文本查询 {'$text': {'$search': 'Mike'}} text类型的属性中包含Mike字符串
: M3 S- s+ S: ?/ x/ h. X( }; a3 {8 o$ _' q c" H
$or 查找多种条件 ({'$or':[{'name':'chen'},{'name':'wang'}]})# c5 m, S9 o4 Z! h
$where 查询两个键的值是否相等 db.test.find({"$where": "this.fields1 == this.fields2"}).limit(10);
4 z6 H$ e( o( E/ i+ J' r) d Y9 D# i6 D# |( y
$exists 键是否存在 {finaGoalLine:{'$exists':true}}0 `; C- e0 z, E7 f9 V
% J$ l9 v$ F& z/ Z$all 如果您希望找到一个包含这两个元素的数组 [ 'tags' => [ '$all' => [ 'red' , 'blank' ]]]/ `* \& @% `4 r) I; k% z
& L6 ^1 c# e$ L& M
$elemMatch 运算符在数组的元素上指定多个条件 'dim_cm' => ['$elemMatch' => [ '$gt' => 22,'$lt' => 30,],],6 d, }+ j! j& y
$size 运算符按元素数查询数组 [ 'tags' => [ '$size' => 3 / L4 s7 I4 Q2 K' s# N W3 S
% V( _! F$ n9 D. P% N( c: J S3 d: n* B6 @* }3 S8 l
( |' C3 L% D! S& o. n2 E
2 `. }) w- z7 G8 R' |
" Z2 u. F2 h) O) G- W- [
# t! t5 S( _% O- G3 U
; V! D6 e, l5 ^! ]0 Z* ^
- V2 F& c' w0 O: u组合使用方法如下:
- q4 E: M7 |5 l" z/ b9 P
5 Y- V# x8 l0 P, N" M$ d( T2 ~db.user.find({"age":{"$gte":18,"$lte":25}})
) T4 X6 D* Z3 i; F. x$ n6 T' |: ]8 C. j% p5 d& Y
8 c- b' c1 X9 d4 L1 H$ y& C# i/ A* B) o
对于日期的条件查询方法:
" r, R+ l& T' R0 e+ H$ E+ V" h Q( ?3 T6 _* X; y3 Z" ^
db.getCollection('news').find({'pub_date':{'$gte':'2017-07-11 11:0:0'}}). M' r+ w) c+ a; G- L5 y9 e/ T8 h
% Z& y' _) h5 g: X: ]2 O
5 q0 s( w$ o9 E- e' a( I7 u! O) Y$ M% }+ }/ X$ i9 T) g! L/ [
2) 不等于 $ne
+ D0 p9 q/ o) U8 _& U4 a3 {# z8 N+ H# ]. q
例子:8 k6 F& m# e! {4 M
+ G- }# R6 L% ^3 bdb.taobao.find( { age: { $ne : 10} } );; ^6 D4 ]3 \1 }6 J7 k& C
php7 查询mongodb方法大全( y6 c0 O, D4 ? [$ B% K
( j8 g( Z7 `' C4 |
: i7 k6 h, f5 m/ P( b( a7 i' i: I& ~" `. O. I
. V) s0 h8 M/ R9 b9 m3 D
) S2 y; u- `% s& ]" x
- H4 C# h* x1 @7 C- J" Z6 T: I8 n
) a* P/ y( w/ N# Q
# q2 P4 @/ L7 h9 E# w2 y b" N( G4 d9 i( E7 i
3 `/ f7 R) e9 a9 {% g6 O
9 j+ D! m5 e, F! f1 N* U" f
3 @9 x1 ~' ~! W7 o |
|