は私が正しくあなたの問題を理解していれば、あなたがからオブジェクトを返すようにしたいとmongoコレクションを作成し、それが満たしている条件とそうでない条件のそれぞれのオブジェクトに情報を添付します。 firstName、lastName、birthYearというフィールドを持つオブジェクトを持つmongodbコレクションの人がいるとします。 {firstNameの: "ノバック"、lastNameの: "ジョコビッチ"、birthYear: "1987"、matchConditions:222}
{firstNameの「ノバク我々は
db.person.aggregate([
{
$project : {
firstName : "$firstName",
lastName : "$lastName",
birthYear : "$birthYear",
matchCondition1 : { $cond: { if: { $eq : [ "$firstName" , "Novak" ] }, then: 200 , else: 100 }} ,
matchCondition2 : { $cond: { if: { $eq : [ "$lastName" , "Djokovic" ] }, then: 20 , else: 10 }} ,
matchCondition3 : { $cond: { if: { $eq : [ "$birthYear" , 1987 ] }, then: 2 , else: 1 }}
}
},
{
$project : {
firstName : "$firstName",
lastName : "$lastName",
birthYear : "$birthYear",
matchConditions : { $sum : [ "$matchCondition1", "$matchCondition2", "$matchCondition3" ] }
}
}
]);
結果は次のようになり、この凝集を使用することができます」とLastName: "ペトロヴィッチ"、birthYear: "1987"、matchConditions:212}
{firstNameの "ノバック" とLastName: "ペトロヴィッチ"、birthYear: "1989"、matchConditions:211}
{ firstName: "Roger"、lastName: "Federer"、birthYear: "1981"、 matchConditions:111}
次にmatchConditionフィールドから、オブジェクトが満たす条件の情報を取得できます。これがあなたの望むものだと私は期待しています。