管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
查询表中学生年级大于20,如下:4 C& o" N4 Z' b3 @. K2 s4 B
O% E( f" Y) Gdb.getCollection('student').find({'age':{'$gt':'20'}})
6 Z: g. T' N! b' {8 y/ c! E5 N3 h+ f; z- Z. A
1 {7 V+ P4 z4 _0 G0 L
& ?# J8 s( r! G" B: t' l% Q$lt < (less than )& d/ ~# D5 N, e. F
9 T2 u0 {+ ?0 j% |' }: }
$lte <= (less than or equal to )
: B$ T6 `$ x) K% R5 a) ]
6 t% ]6 ]5 I8 H5 Y. V5 v/ S# ]) e( m$gt > (greater than )9 `1 f! U, d/ o8 i* ~
# f: Q& A; o: g0 |
$gte >= (greater than or equal to)
# M9 E$ P4 }" P; F6 W3 v) ~7 [! m% V. J. g# N
- z) y4 e' n; T4 m
' U! P1 W% o$ b$ne != (not equal to)不等于 {'age': {'$ne': 20}}
+ F$ |4 N4 |7 o8 m# k2 l% j3 u2 I- n! l
$in 在范围内 {'age': {'$in': [20, 23]}} 注意用list( v$ q8 j5 s" y* }& y
. `( L' v7 Y3 }
$nin (not in) 不在范围内{'age': {'$nin': [20, 23]}} 注意用list
. |4 \5 J! n7 l! X2 t% ?
. P) j" U4 H+ m' v0 C0 q! f$regex (正则匹配) db.collection.find({'name': {'$regex': '^M.*'}}) 匹配以M开头的名字: I1 o' ~* p! j* |
( \3 C) H: I" e* s) F0 |% `$ G
$exists 属性是否存在 {'name': {'$exists': True}} 查找name属性存在
; w# M& Z* {% l- ?' n. k2 c+ }: c7 g; w8 i; h
$type 类型判断 {'age': {'$type': 'int'}} age的类型为int
5 c% z" m0 w; z2 D/ q8 b5 l/ L# t. @# i( N5 M0 R- L9 V
$text 文本查询 {'$text': {'$search': 'Mike'}} text类型的属性中包含Mike字符串' r! R+ ?/ v( h; ]0 }* h' ~' u
5 u. y% `0 |- N1 A" f9 ]: o
$or 查找多种条件 ({'$or':[{'name':'chen'},{'name':'wang'}]})
! j& c4 Z; P" o1 F7 N* l8 ?3 V2 |$where 查询两个键的值是否相等 db.test.find({"$where": "this.fields1 == this.fields2"}).limit(10);
; E0 d# V# {: w2 P; s, G0 {2 H% y, N% T8 j4 y, a
$exists 键是否存在 {finaGoalLine:{'$exists':true}}
# \ n4 W7 q/ u' Z5 W, Q4 I7 G( X- B( d
$all 如果您希望找到一个包含这两个元素的数组 [ 'tags' => [ '$all' => [ 'red' , 'blank' ]]]# p8 Z A( N1 H8 W( V4 Z
: x2 |+ M; E& |6 o
$elemMatch 运算符在数组的元素上指定多个条件 'dim_cm' => ['$elemMatch' => [ '$gt' => 22,'$lt' => 30,],],
6 w/ H$ Q% A; C) W7 k7 @6 k$size 运算符按元素数查询数组 [ 'tags' => [ '$size' => 3
( G& q- G* Q" f+ p! T2 `" B! O; V8 T4 @& l# T5 l
' S: F9 s' u: G4 Y& Q2 t
# |; [' }0 T' R
5 u) _% G- p' H# F4 R" A3 [$ R) |& j
8 r% Y R9 l; h8 x. e" B
+ j7 b2 J2 [/ v x$ {% k
3 ]0 ^" Z0 _! ^5 \
7 N/ v; c; I r* I! Q; ^- s/ p& @
组合使用方法如下:
* M6 t" V! b5 C, M' R" L; ?, E9 \
' l1 w- c( h4 p6 Z9 ldb.user.find({"age":{"$gte":18,"$lte":25}}). L! z) ~- |6 M0 ]8 B: U4 e/ c$ b
' T0 O u: i3 h: d; |6 f- D
0 h2 L. J' |* g$ _ V6 e& S; S! H1 B9 X9 O# F) W
对于日期的条件查询方法:
( U# \$ ?* v6 ], F+ I0 i
4 U2 y4 M5 F8 D- _$ t; n& qdb.getCollection('news').find({'pub_date':{'$gte':'2017-07-11 11:0:0'}}); D: w% D; I2 @
0 \9 j$ h8 \% I: f' A; I! f7 O8 c* ^0 `& \9 l( `1 G9 ~& ~2 C
+ i$ @) d( p% S1 A" r
2) 不等于 $ne
6 e# P9 T0 Q+ A6 p
4 s( \5 j9 x* H" |* |例子:
9 z9 x$ S) L/ y- A% j9 }6 V$ G
* I \2 P4 j$ C7 T' `. m6 ndb.taobao.find( { age: { $ne : 10} } );+ g N# }+ k$ b8 c$ P' N2 o8 L
php7 查询mongodb方法大全
/ ^# C3 K' [7 D) q" u5 M
0 Q4 U' w6 T2 I3 o( {5 C1 s
7 F/ R8 N# z- ]' F: b( P. O( e) j1 \- x1 t+ g- Z% ~
- h- ]. |9 s! t- V; T
* {# m) {+ ^/ e, j3 t! `
, d0 {- i0 q+ y2 k& q6 V2 g
# g* G' U( p9 e( Q$ h
2 k/ s4 c9 z. G( A, r; o4 j* n6 V0 L' k' @
( f6 _6 u p. w3 l; V4 s+ ?7 Q3 V' C/ {. Y" P" c
# a+ a' ]+ _: }. J
|
|