2017-11-28 3 views
0

funding_roundsは配列です。次のクエリを実行してエラー$size needs a numberを取得しようとしていますが、$size$gteを組み合わせて使用​​する正しい方法は何ですか?

db.companies.aggregate([ 
    { 
     $match: { 
      $and: [ 
       {"founded_year": 2004}, 
       {"funding_rounds": {$size: {$gte: 5}}} 
      ] 
     } 
    } 
]) 

答えて

1

$サイズオペレータ突起内部で使用されなければならない、あなたは、マッチングの前に、あなたの文書を変換する必要があるので:

db.companies.aggregate([ 
    { 
     "$project": { 
      "_id": 1, 
      "founded_year": 1, 
      "funding_rounds": 1, 
      "funding_rounds_size": { "$size": "$funding_rounds" } 
     } 
    }, 
    { 
     "$match": { 
      "$and": [ 
       {"founded_year": 2004}, 
       {"funding_rounds_size": { "$gte": 5 }} 
      ] 
     } 
    } 
]) 

配列の長さを比較するためにはるかに短い方法もあります:あなたは、第五かどうかを確認することができます要素が存在する:

db.companies.find(
    { 
     "founded_year": 2004, 
     "funding_rounds.4": { $exists: true } 
    } 
) 
関連する問題