管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
查询表中学生年级大于20,如下:
; U: w- _! f G2 Q, a
9 Q: G6 v4 f. i# ^db.getCollection('student').find({'age':{'$gt':'20'}})
8 g4 \! R, D) v6 y- M- O
1 W/ r, S% G' Y1 E6 E8 V5 Z7 k* ~0 l3 A5 [2 ?2 G
* _: D$ s; x7 J5 u$ d% l' U& O& Q
$lt < (less than )
2 j3 O& u9 m9 Q$ k4 r
: Y- f. u! r+ D/ ^* R. m& d$lte <= (less than or equal to )6 @: v, s9 o( M
0 Q# B% i% s3 {4 n" V; Y$gt > (greater than )* M1 a2 Z' E7 V0 t. A; D; F
& q a' M8 \) D+ D# A* T5 l; G* [
$gte >= (greater than or equal to)0 R0 M t% u5 O# Z1 l
0 ~ d- K2 z7 ?
' F4 o8 z# B# K: n2 p! p6 u) D$ @4 T; D0 Q! T. G, f
$ne != (not equal to)不等于 {'age': {'$ne': 20}} u. h9 `. M+ R2 U
8 S0 Z: G+ F: ^
$in 在范围内 {'age': {'$in': [20, 23]}} 注意用list
3 L$ B+ b6 x/ j0 l7 D" u
) I* s/ ?. @5 q2 N4 c$ o$nin (not in) 不在范围内{'age': {'$nin': [20, 23]}} 注意用list& T" k/ W8 ]3 T) Y! i8 \
+ h" G7 `7 V- K0 p( |. G, I
$regex (正则匹配) db.collection.find({'name': {'$regex': '^M.*'}}) 匹配以M开头的名字) w2 ~0 s) |- s4 t
# _& P' Z0 b! q9 |1 J K7 d) I; L3 V
$exists 属性是否存在 {'name': {'$exists': True}} 查找name属性存在
* B! Z' z: E8 j- o) D3 i8 v3 h8 a4 @) n8 V1 ]$ P
$type 类型判断 {'age': {'$type': 'int'}} age的类型为int6 i+ v8 I3 L4 y
; h4 D: C% G0 @4 |( L' ]
$text 文本查询 {'$text': {'$search': 'Mike'}} text类型的属性中包含Mike字符串
, X+ x2 t6 \7 {6 T5 h- x8 H. ]# b1 u
$or 查找多种条件 ({'$or':[{'name':'chen'},{'name':'wang'}]})
; n! d6 Y Y- a& ]& P2 l$where 查询两个键的值是否相等 db.test.find({"$where": "this.fields1 == this.fields2"}).limit(10);
. p+ A4 e( p) `1 r. A; d$ I
( q) Z# q/ Z3 s; q7 S9 u# C$exists 键是否存在 {finaGoalLine:{'$exists':true}}
5 x" K0 m3 x' d! x! Q% e m) P9 C& `% U# ~7 Y) H- u' c/ W/ m
$all 如果您希望找到一个包含这两个元素的数组 [ 'tags' => [ '$all' => [ 'red' , 'blank' ]]]
% a) h! |) N+ h# a/ e* x9 p
6 S" ]9 ~# U9 Q: f3 L$elemMatch 运算符在数组的元素上指定多个条件 'dim_cm' => ['$elemMatch' => [ '$gt' => 22,'$lt' => 30,],],
$ {: K# y4 u5 i3 `) Z2 j$size 运算符按元素数查询数组 [ 'tags' => [ '$size' => 3 ) O# F8 Q' f; B4 y$ O
5 D! x$ L' v( w
: c* I) q" M9 |1 {/ O. ?
( L0 R1 F/ P8 `9 [7 m5 B' e- }5 E
3 P$ A% |# w& s3 o
9 z5 y* U2 O# h
- b! a A. H- R
1 F# x4 o1 B5 f+ r# u$ `组合使用方法如下:
! v- z- v: n! g3 r5 ?
" x! q8 r" O7 {9 kdb.user.find({"age":{"$gte":18,"$lte":25}})6 e7 N8 X8 t$ g$ o% s
/ P1 h7 ? x+ Q- V+ k, a
- D6 ?: c9 m6 o
" ]5 p* M3 }. X8 j& S$ g5 |
对于日期的条件查询方法:6 U3 v0 l: z$ {. P* e( Y5 @: U! x
2 M) K3 f& [! o2 j1 S- T6 @
db.getCollection('news').find({'pub_date':{'$gte':'2017-07-11 11:0:0'}})
6 |6 P1 }& Q/ d8 X* q4 t
# {/ w2 [: V- z8 Z, \( @2 [9 k' o) ] l; r7 s/ p: B |8 S
3 E* i! x' B) i$ Q# y: G$ v9 d- _2) 不等于 $ne
; t5 s0 S. g/ H2 A) O# N1 `
4 o9 p8 F- s f2 S例子:2 q6 u4 q6 _. E* E
8 X- y- P, B# v( l8 D( Idb.taobao.find( { age: { $ne : 10} } );
0 t9 y) R! F7 K6 k0 W7 vphp7 查询mongodb方法大全
% f8 e! f7 \- G9 M; t( p, w
0 Y! z+ J$ v1 V( O- T+ y8 C' Q2 I. S: b* x9 [" K% A0 H! e
) p1 X0 n2 B- V* V( V" L
$ O/ h Z. {) {
- T+ H3 O3 k- A& b6 W) { n/ o
6 ?2 T' c5 {# k- B, N0 [: ^0 ]3 }5 B V0 g: s7 t& _. ~
a# C/ n( ]/ p& V* B
/ a9 b* a+ F: t" N% W( _5 ?4 C) D/ X. Y" O, t, C& q
% h" Y- Q3 Q S- I
* d" F. t5 ^4 B, F a$ R |
|