管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
查询表中学生年级大于20,如下:
0 ^7 D( c4 ]5 h3 L) i( n
5 L% Z) u! [3 b; s$ X+ }8 kdb.getCollection('student').find({'age':{'$gt':'20'}})6 P. G$ L! U/ B4 ^# H
* P3 q1 V! Y3 ^9 L1 N
& T! X- y1 ~) D- s% i& }
1 s) D t; q+ [: }" M' d% e$lt < (less than )
5 a" u# \) F3 r; n2 m8 t
" ]* r2 D. ~& w3 u% l2 g$lte <= (less than or equal to )
$ s4 S; t, F# z2 A k8 C
& O6 s: n% x( N5 v1 E" G$gt > (greater than )) g& z! m3 z2 ^' ~
7 \! u; G9 j6 j$gte >= (greater than or equal to)
- K6 O0 _; R9 v% h5 I; f/ C8 b, e$ M& F8 `9 v7 {8 m
7 g7 N6 \# n& }# }* t( B
7 h% d5 `( E* E3 n9 N, d4 c
$ne != (not equal to)不等于 {'age': {'$ne': 20}}
, y9 ]5 H: Y j0 |) R' i9 _, c! S t2 J+ J( j2 P9 M
$in 在范围内 {'age': {'$in': [20, 23]}} 注意用list
3 X' f% A! D* q
]* x, f+ y+ z- R0 o) o$nin (not in) 不在范围内{'age': {'$nin': [20, 23]}} 注意用list( u9 E3 Q( X0 i
; b9 N: \- m) I. Q( A/ r
$regex (正则匹配) db.collection.find({'name': {'$regex': '^M.*'}}) 匹配以M开头的名字, {: {( o4 \$ g
! I! s6 q6 D7 E+ g3 Y. e$exists 属性是否存在 {'name': {'$exists': True}} 查找name属性存在
' s* c% U& u X! o# {9 `9 x
/ A( i; G' b; _3 y5 R$type 类型判断 {'age': {'$type': 'int'}} age的类型为int
- s4 ^+ s4 [2 a' K
9 i1 x6 G8 x9 X! e% }6 w* b$text 文本查询 {'$text': {'$search': 'Mike'}} text类型的属性中包含Mike字符串
$ p" c6 E: M1 c/ E: Y7 p5 f0 z3 ^7 e7 ~
, Z- s+ k z/ @% u3 A$or 查找多种条件 ({'$or':[{'name':'chen'},{'name':'wang'}]})0 B5 s" D% R u2 g8 c% r' ]' s
$where 查询两个键的值是否相等 db.test.find({"$where": "this.fields1 == this.fields2"}).limit(10);
0 y _5 S1 J ~5 f+ Z2 j* V' _1 H7 q; A. Q, P% X: ]/ j' C
$exists 键是否存在 {finaGoalLine:{'$exists':true}}
5 q+ [ c! C, r& Q) m
2 c7 S, s! D9 W2 O8 S- j# C$all 如果您希望找到一个包含这两个元素的数组 [ 'tags' => [ '$all' => [ 'red' , 'blank' ]]]
6 c; G0 v4 N; E7 u1 j: v2 Y
6 y3 b6 d; U0 a# G9 h$elemMatch 运算符在数组的元素上指定多个条件 'dim_cm' => ['$elemMatch' => [ '$gt' => 22,'$lt' => 30,],],
$ l! k* ^& f( k; z7 u$size 运算符按元素数查询数组 [ 'tags' => [ '$size' => 3
3 S; O' a! Z1 j
: a: C4 D2 Z7 K, r
9 P# ^. B+ _" u# K
# {! x3 C7 j0 }0 u4 r& o* d9 {9 W3 y% _" z$ ]" {
/ @' a" s9 l4 c( Z
5 M* ?, c$ _$ L3 O+ \+ W9 o
7 f6 N5 ~7 \1 Y' k
' n: Z; k+ f3 _" D1 D' Y组合使用方法如下:
4 ]. T( Y# O' V, y8 V( _1 q' d' _* h4 M7 V! v2 {/ i6 R9 O$ {" A+ i
db.user.find({"age":{"$gte":18,"$lte":25}})
2 [1 y/ z1 n ?- J* D2 M( D+ C4 ?/ \( G( u' Y) W+ U
4 @0 X- O, @; _4 M2 p0 {
9 a' P$ f! i3 a8 y1 H3 f
对于日期的条件查询方法:
, Y* a1 O. W7 Q& M6 w. Q: [0 h7 t a& s+ l+ N
db.getCollection('news').find({'pub_date':{'$gte':'2017-07-11 11:0:0'}})5 t( A1 T! W- V# T5 U, p
' u* V7 R* N' X$ x
8 _& x: W9 q/ L2 i3 d) R7 _2 e* A3 l
2) 不等于 $ne3 p+ i( N! A. d( K J- y
$ h% Y; V4 @: v
例子:& }& m) Q# J" |- R7 z. \
" j, |, B9 ?$ N8 V+ z2 k; X
db.taobao.find( { age: { $ne : 10} } );
' s' b4 ^, Y8 aphp7 查询mongodb方法大全
# F# _ @* s* G5 c# w1 ], A0 x, R( Z: T. i' `/ j
. K" m! K; v9 O* N% N: G$ f
' C' {+ ~, m5 p: v6 D7 d- T+ [, W6 R4 j/ p
7 c& P! A3 f1 }/ s- w3 s- [
3 t3 l5 M' P/ F' v
* g+ Y) ^, F# n3 \) j: f0 X0 V& i1 f) Z4 T/ |
: |2 g% R! \+ O4 O
- h) {/ A- T8 {$ n' [
0 e( u* t2 |+ J8 l" a M1 g
) y6 c! V$ Y& R3 E) d" U
|
|