管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
查询表中学生年级大于20,如下:
' \% m# \ m6 E2 ]0 n0 ~) k9 O% J( A* p+ ]$ t7 Z: u
db.getCollection('student').find({'age':{'$gt':'20'}})) S3 N8 C) V" U+ O! a. f3 z
& y4 i4 z4 D) N+ `8 y
1 t/ o! \2 N$ W K* z
5 ~+ w* k* C: O: x# A9 @. g$lt < (less than )5 Y3 m( E g( @4 S* ^+ F
; J3 u/ ^0 I; V/ ?1 A$lte <= (less than or equal to )
5 i2 p! J! N6 `( P, l5 Q$ V
2 t5 E' o& K! Y* z! G# i$gt > (greater than )
7 E; ~: {( `4 R6 k5 Q/ w: ~' }, m
% O8 Q' A: M# Q7 P6 Z$gte >= (greater than or equal to)
* s0 z/ E! Z& F. |% B3 K9 u1 C: r$ E) y
5 @. {/ q% ^0 {) p
4 V: X( U9 z: J$ne != (not equal to)不等于 {'age': {'$ne': 20}}: v- L! ]6 V7 _' Z7 c o, y' L0 X. t
7 A) T) m2 O3 S2 Q! U
$in 在范围内 {'age': {'$in': [20, 23]}} 注意用list
8 m( a: Q% ~& d4 M3 U2 A$ |7 c: P& R# t; {5 ~$ j- o0 m
$nin (not in) 不在范围内{'age': {'$nin': [20, 23]}} 注意用list
: R/ @5 C: }3 z$ ]! q1 D5 T) x
: e- f* H/ }5 _! E0 p7 T/ K$regex (正则匹配) db.collection.find({'name': {'$regex': '^M.*'}}) 匹配以M开头的名字! {1 O6 R& f# c
3 S3 j6 \; j! [
$exists 属性是否存在 {'name': {'$exists': True}} 查找name属性存在
: J8 s8 {' L" H8 R% X( z T, b& ]8 v4 y
$type 类型判断 {'age': {'$type': 'int'}} age的类型为int
: E0 B0 s$ e# W Y, I
, f; w2 l# c9 {& i) Y$text 文本查询 {'$text': {'$search': 'Mike'}} text类型的属性中包含Mike字符串
, Z- U! I* n" |$ M- T: f+ L1 M( b/ g- Z
$or 查找多种条件 ({'$or':[{'name':'chen'},{'name':'wang'}]}) {" Q, e0 A+ Q0 C* T3 T: e! m7 T" ?1 x
$where 查询两个键的值是否相等 db.test.find({"$where": "this.fields1 == this.fields2"}).limit(10);
+ V, T- Q+ N# s/ c4 Q! M* j. p9 H; l# ^
$exists 键是否存在 {finaGoalLine:{'$exists':true}}
5 f7 ?" l! e* S) C! j! E9 m; s" A: {! z; m8 x
$all 如果您希望找到一个包含这两个元素的数组 [ 'tags' => [ '$all' => [ 'red' , 'blank' ]]]2 K9 }* N. x( h- e
* Y1 n0 H8 F# v* Z* w$elemMatch 运算符在数组的元素上指定多个条件 'dim_cm' => ['$elemMatch' => [ '$gt' => 22,'$lt' => 30,],],
' E3 K: F5 V5 n$size 运算符按元素数查询数组 [ 'tags' => [ '$size' => 3
, C$ u3 \5 g5 Z) @: _6 P: E
: i0 t2 r. l1 n' a+ q9 v3 Y& U: T( @/ Y4 o3 B
$ T$ y0 @9 r4 @/ X! ]7 x9 Q- k' c; S# S( r0 c2 b
c; y6 v# q# F* f& P- `" ^ s1 `, D/ U1 z! U; m
: \. [, ~9 ~9 C) l/ _
' R4 h$ Q j1 v/ O, l8 {' n7 y
组合使用方法如下:
1 ^# y$ a" f' R4 g" F& Q7 V6 n5 o( Z4 \0 L; N; R0 u: o
db.user.find({"age":{"$gte":18,"$lte":25}})
[3 x% n3 P+ j* @' E4 ?, m
; @. T$ a# e. i# K8 a6 o+ }: j5 g% q3 {. ~- q
+ ~) ~% [& o# b% g" T) f对于日期的条件查询方法:
; M( S. k% ^+ u, ]) i
" O3 y; p! b, K5 l- qdb.getCollection('news').find({'pub_date':{'$gte':'2017-07-11 11:0:0'}})1 r0 `- B+ L" v9 a
! P, j$ Z9 T4 s4 h) X5 T; A- ]" h: \
6 G6 P4 a' f, q5 M9 J2) 不等于 $ne/ I. c* r0 {# g; ~3 @) L3 E
# h* V+ I) q' z5 C8 i# ~' c/ T- f例子:8 n3 G7 b# F8 D& l- v N( |) J- W
1 W, E5 w2 c9 `- t( Kdb.taobao.find( { age: { $ne : 10} } );
5 ^! H5 ?4 w. x4 q" Ephp7 查询mongodb方法大全& A8 V& J& q* I% s; J" r u# a: z
6 @+ F& Z) Q% T
4 l" P' z4 h) A' {4 L8 C
9 [; i) l4 N3 S `7 o5 \1 k# b" d% B4 `6 `/ N
( N+ d$ U. \ j8 \/ `) S6 W8 v
( D* `1 I: N$ S+ b, V# U
' f/ d- Z' w# b4 s7 j0 k; `! g! p
) h! T, ~$ C; z% \1 Y6 [4 M) N( A& j8 k' g* g$ f6 u/ C+ i0 n. c
3 k0 O0 X6 K( I
7 Q* I% G w7 j. N Y' y
|
|