2011-09-14 3 views
0

MongoDB 1.8.1을 사용하고 있습니다. JavaScript 배열에 표준 some 메서드를 사용하여 $where 절 내에 Mongo의 $elemMatch 쿼리를 에뮬레이트하고 싶습니다. 그러나, 비록 내가 더미 함수를 제공하더라도, 쿼리는 결코 어떤 것도 매치하지 않습니다. 하여 MongoDB에서

> db.foo.insert({bar: [1, 2, 3]}) 
> db.foo.count({$where: 'this.bar && (this.bar.length > 0)'}) 
1 
> db.foo.count({$where: 'this.bar && this.bar.some'}) 
1 
> db.foo.count({$where: 'this.bar && this.bar.some(function() {return true;})'}) 
0 

자체 쉘, 그것을 작동 :

> [1, 2, 3].some(function() {return true;}) 
true 

는 왜입니까?

답변

2

그냥

> db.foo.count({$where: 'return this.bar && this.bar.some(function() {return true;})'}) 
> 1 
> db.foo.count({$where: 'return this.bar && this.bar.some(function(bar) {return bar>2;})'}) 
> 1 
> db.foo.count({$where: 'return this.bar.some(function(bar) {return bar==1;}) && this.bar.some(function(bar) {return bar==6;})'}) 
> 0 

를 작동하여 위치를 쿼리하기 전에 수익을 추가하지만 난 그게 전부 어디 claues에서 대신 함수 문자열을 사용하는 것이 더 깨끗한

>db.foo.count({$where: function() { return this.bar.some(function(bar) { return bar > 1;}) & this.bar.some(function(bar) { return bar > 2;}) }}) 
>1 
관련 문제