管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
查询表中学生年级大于20,如下:
# N6 W" N1 W6 v4 x' V$ r" N
6 ]# d# b/ {: P: c: adb.getCollection('student').find({'age':{'$gt':'20'}})
7 A" ], ]- w7 C( A
+ T1 O. C0 F `5 Z! {; x5 W. ^( n$ m$ ^) a6 _: x: \
1 X3 k, X0 P1 z6 b C' g. `
$lt < (less than )+ k( G, q& M7 K# Q' a
+ r2 ~5 y( A1 }# Y& |$lte <= (less than or equal to )- J4 s" ^/ s5 \8 ^) M' b
) s+ ~; D% \# W/ T$gt > (greater than ): @+ C8 D- y. m4 a6 [
' x' }$ t: T& D8 i' J
$gte >= (greater than or equal to)2 ]- @1 ?8 j9 F
! a p" D0 g. _. ]" b) n2 W
2 c% a4 e! y9 g- M" F6 b( x7 y
8 |% _! G6 ^" R( `$ne != (not equal to)不等于 {'age': {'$ne': 20}}1 b3 x: |7 G4 S* ` Q! C; G
4 P/ y! \- H g- C$in 在范围内 {'age': {'$in': [20, 23]}} 注意用list
7 M& [9 m" C, a' K
0 p. l0 q. I& f) l8 x+ z+ S$nin (not in) 不在范围内{'age': {'$nin': [20, 23]}} 注意用list
- L% ^( |0 U O# \+ n; m
. H9 N* f- [: f0 S/ R( t% y5 K$regex (正则匹配) db.collection.find({'name': {'$regex': '^M.*'}}) 匹配以M开头的名字
" W6 [% d, r' _. ]$ ]" i, ~ F0 V. i! O
$exists 属性是否存在 {'name': {'$exists': True}} 查找name属性存在4 Z- W8 J n0 o0 `0 b' B
- N& |/ U; W' O1 x$type 类型判断 {'age': {'$type': 'int'}} age的类型为int& l1 h) ?0 X0 R" ^# W
' f0 C2 m# ]+ T$ S
$text 文本查询 {'$text': {'$search': 'Mike'}} text类型的属性中包含Mike字符串- {6 ^$ o5 M( \2 @0 V; P/ }
/ T3 R9 M, P2 ]$or 查找多种条件 ({'$or':[{'name':'chen'},{'name':'wang'}]})# |$ R O. m5 A/ u' r+ t
$where 查询两个键的值是否相等 db.test.find({"$where": "this.fields1 == this.fields2"}).limit(10);) \0 B4 w) _: f
7 _) m! u$ F: z4 a) C$exists 键是否存在 {finaGoalLine:{'$exists':true}}3 B" Y; a; z4 @
9 p; c, J7 z0 f) b; s+ J
$all 如果您希望找到一个包含这两个元素的数组 [ 'tags' => [ '$all' => [ 'red' , 'blank' ]]]! S8 S+ }! o( j# r, D
9 d5 j5 G6 l# s s
$elemMatch 运算符在数组的元素上指定多个条件 'dim_cm' => ['$elemMatch' => [ '$gt' => 22,'$lt' => 30,],],; K" ~4 g( l! {, f
$size 运算符按元素数查询数组 [ 'tags' => [ '$size' => 3
7 I' D& f; R( @6 u- L* }( M9 ^0 C1 C/ |
+ [5 Q# j- h$ Z0 X& v2 ]7 j9 p9 m6 Z7 ], m% L$ ~7 I1 m, k# w
3 `1 M$ T0 H+ R7 \+ }
- q, D( ?' V2 F2 h. J9 H2 u
3 r# [9 e4 ?& P J) `; t. t& t. f8 Q5 R& C
1 L- D% s# r+ Q; X. M) a* T组合使用方法如下:
: J8 Y, ]+ d5 H& A6 k$ S# u+ m
7 y/ L3 E, s/ f8 a9 v, y% F7 E6 D* Cdb.user.find({"age":{"$gte":18,"$lte":25}})
7 `: W+ Z# U8 l" ]2 E% Q4 N/ }$ R# ^2 }4 N" [* v: g0 z! {
, T. n6 B. {) m
V5 u4 S6 ]# K对于日期的条件查询方法:' U& {$ ?/ p. E" C
" z, T" P+ A, v+ H$ M7 k) s
db.getCollection('news').find({'pub_date':{'$gte':'2017-07-11 11:0:0'}})+ m" Q9 S) C/ y& r b! n- Y
3 H* c3 |5 N1 D. t/ a" ?/ H" R7 W. n. U
: a3 u. t) O! a, U% [+ e# E& m( P; s2 Y
2) 不等于 $ne' t: N( c% @' I4 f8 k6 q F
* `4 L% l; L( L4 S0 [例子:
" H* B# j ~! V4 G8 I( Z
$ V6 ~$ z6 m# O4 Q+ q' hdb.taobao.find( { age: { $ne : 10} } );1 z' m: d7 K0 d7 V# _
php7 查询mongodb方法大全& \" M3 N* b* E! H, I
4 b+ ]. Z" m" Q6 W, L# i' h0 I4 \% t5 }: H7 V# u
7 J: n7 Z* M" N/ r* \
, m1 c5 l$ D! j1 q
1 J' Y: O: ]: h" v. w
1 a! i; r# F2 \# F& [( A1 S q( G Z! M7 Z0 y. k' G1 x. p: z
, I" k( m, q/ q! D* }) w" T/ u
1 K: D( z) Z# Q' z/ w
' X; j% u6 J M p$ u1 ]* x
2 |& ?5 l/ Y2 k/ ^3 B2 d9 _5 R6 U0 d3 J; _% K
|
|