MongoDB Querying Operators


$gt 와 $lt ($gte 와 $lte)


기본

find나 findOne의 first argument document에

field의 value로 object 를 넣어주고, $gt, $lt 값을 설정해준다.


$gt : greater than $lt : less than $gte : equal + greater than $lte : equal + less than ex)
  • db.scores.find({score:{$gte:95,$lt:98},type:"essay"});
이렇게하면 

score는 95이상 98 미만이면서 

type은 essay인 결과들을 뽑아낸다.


$gt와 $lt는 문자에도 적용된다.


$gt , $lt는 문자에도 적용될 수 있다. ex) db.people.insert({name:"Alice"}); db.people.insert({name:"Bob"}); db.people.insert({name:"Charlie"}); db.people.insert({name:"Dave"}); db.people.insert({name:"Edgar"}); db.people.insert({name:"Fred"}); db.people.find({name:{$lt:"D"}); 로 하면 D 미만의 것들이 나온다 (Alice,Bob,Charlie)



$regex, $exists, $type $exists

document에 해당 field가 존재하는지 찾는다.

true일 경우 해당 field가 존재하는 것을 찾고,

false일 경우 해당 field가 존재하지 않는 것을 찾는다.

기본구조


  • db.people.find( { field : { $exists : boolean } );


ex)

  • db.people.find( { profession : { $exists : true } );
이는 profession이라는 field가 있는 document를 모두 출력할 것이다.

반대로,
  • db.people.find( { profession : { $exists : false } );
이는 profession 이라는 field가 없는 document를 모두 출력할 것이다.

$type

document의 해당 field가 특정 type인지를 찾는다.

기본구조

db.people.find( { field : { $type : 특정숫자(타입번호) } );

ex)

db.people.find( { name : { $type : 2 } );

은 name의 type이 String인 것을 찾는다.

* $type : 2 -> 2번은 String Type을 의미.


$regex
mongodb는 $regex도 사용가능하다. 추후에 자세히... db.people.find( { name: { $regex: "a" } } );