jason0401 发表于 2015-12-22 15:21:06

mongodb系列

  文档的查询:
   
  1,集合查询方法 find()  查询集合中文档并返回结果为游标的文档集合
  
  
  查询集合中所有文档:
  db.collection.find()//或者db.collection.find({})
  
  筛选符合条件的文档:
  db.user.find({age:20})
  
  2,条件操作符查询
  db.collection.find({"field" : { $gt: value } } ); // 大于value
  db.collection.find({"field" : { $lt: value } } ); // 小于value
  db.collection.find({"field" : { $gte: value } } ); // 大于等于value
  db.collection.find({"field" : { $lte: value } } ); // 小于等于value
  多个条件:
  db.collection.find({"field" : { $gt: value1, $lt: value2 } } );
  
  
  3,$all 匹配所有
  
  db.users.find({age : {$all :}});
  {name: 'David', age: 26, age:[ 6, 8, 9 ] }
  
  4,$exists 判断字段是否存在
  查询存在字段name 的数据:
  > db.c1.find({name:{$exists:true}});
  { "_id" :ObjectId("54ded6690dc51419494b4564"), "name" :"jack" }
   
  5. $mod 取模运算
  查询age 取模10 等于0 的数据
  db.student.find( { age: {$mod : [ 10 , 1 ] } } )
  
  6,$ne 不等于
  查询age 的值不等于17 的数据
  > db.c1.find( { age : {$ne : 17 } } );
  
  7,$in 包含
  查询age 的值在18,25 范围内的数据
  > db.c1.find({age:{$in: }});
  
  8,$nin 不包含
  查询age 的值在18,25 范围外的数据
  > db.c1.find({age:{$nin: }});
  
  9,$size 数组元素个数
  对于{name: 'jack', age: 25,favorite_number: [ 5, 6, 7 ] }记录
  匹配db.users.find({favorite_number:{$size: 3}});
  不匹配db.users.find({favorite_number:{$size: 2}});
  
  10, 正则表达式匹配
  查询name 不以N 开头的数据
  > db.user.find({name: {$not: /^N.*/}});
  { "_id" :ObjectId("54ded6690dc51419494b4564"), "name" :"jack" }
  
  11, $where 查询
  查询a 大于10 的数据
  db.c1.find( {$where: "this.a > 3" } );
  
  12, count 查询记录条数
  查询user表的数据量
  > db.user.count()
  3
页: [1]
查看完整版本: mongodb系列