3.4.2.8 条件过滤

关系型数据库开发对于数据的筛选,想到的一定是where语句,MongoDB里面提供的是"$where"。

范例:使用where进行数据的查询

db.students.find({"$where" : "this.age>20"}).pretty()

db.students.find("this.age>20").pretty()

这里的this表示逐条的判断。

MongoDB(课时13 where条件过滤)

对于“$where”是可以简化的,但是这类的操作是属于进行每一行的信息判断,实际上对于数据量较大的情况并不方便。实际上以上的代码严格来讲是属于编写一个操作的函数。

db.students.find(function(){

  return this.age > 20;

}).pretty()

MongoDB(课时13 where条件过滤)

db.students.find({"$where" : function(){

  return this.age > 20;

}}).pretty()

MongoDB(课时13 where条件过滤)

以上只是查询了一个判断,如果要想实现多个条件的判断,那么就需要使用“and”连接。

db.students.find({"$and" : [

  {"$where" : "this.age > 19"},

  {"$where" : "this.age < 21"}

]}).pretty()

MongoDB(课时13 where条件过滤)

虽然这种形式的操作可以实现数据查询,但最大缺点是将MongoDB里面保存的BSON数据变为JavaScript的语法结构,这样的方式不方便使用数据库的索引机制。

 

相关文章:

  • 2022-01-03
  • 2021-05-31
  • 2022-12-23
  • 2022-12-23
  • 2022-03-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-15
  • 2022-02-08
相关资源
相似解决方案