2016-04-01 15 views
2

なぜこれが起こっていますか?この違いについて論理的な説明はありますか?mongodbシェルとnode.jsの同じクエリが異なる

たとえば、私はdb構造を持っています。

{ 
    id: "1" 
    category: { 
     name: "name1" 
     groups: [ 
     { 
      groupName : "groupName1" 
      title: "" 
     }, 
     { 
      groupName : "groupName2" 
      title: "" 
     } 
     ] 
    } 
} 

クエリは次のとおりです。

db.collection.aggregate({$unwind:"$category.groups"}, 
         {$match:{"category.groups.groupName": "groupName2", 
         "category.name" : "name1"}}) 

mongoシェルでは、次のようになります。

{ 
     id: "1" 
     category: { 
      name: "name1" 
      groups: [ 
      groupName : "groupName2" 
      title: "" 
      ] 
     } 
    } 

node.jsのクエリ。

db.collection.aggregate({$unwind:"$category.groups"}, 
         {$match:{"category.groups.groupName": "groupName2", 
         "category.name" : "name1"}}). 
         toArray(function(err, result) { 
    if (result) { 
    debugger; 
    var res = result; 
    } 
    }); 
}; 

ここで、node.js結果はlikeです。 Node.jsのドライバで

{ 
    id: "1" 
    category: { 
     name: "name1" 
     groups: [ 
     { 
     groupName : "groupName1" 
     title: "" 
     }, 
     { 
     groupName : "groupName2" 
     title: "" 
     } 
     ] 
    } 
} 
+1

あなたはこのように動作しているサンプルクエリを提供できますか? –

+0

違いは何ですか、より明示的にする必要があります –

+0

あなたの提案をお寄せいただきありがとうございます。 –

答えて

2

、あなたは配列としてではなく、別のパラメータとしてあなたaggregateパイプラインを渡す必要があります。

だから、それは次のようになります。

db.collection.aggregate([{$unwind: "$category.groups"}, 
         {$match: {"category.groups.groupName": "groupName2", 
            "category.name": "name1"}} 
         ]).toArray(function(err, result) { ... }); 

shell versionはより寛容であるが、あなたがそうでなければoptionsパラメータを含めることができないように安全であることを、あなたは常に配列を使用する必要があります。

関連する問題