1

我有UserSchema其中包含PhoneNumberSchema,如下所示。猫鼬用户通过电话号码批量搜索

var PhoneNumberSchema = new Schema({ 
    country: { 
     type: String 
    }, 
    country_code: { 
     type: Number 
    }, 
    local_number: { 
     type: Number 
    } 
}); 

这里是phone_number的示例json格式。

"phone_number": { 
    "country": "US", 
    "country_code": "1", 
    "local_number": "04152341" 
} 

我想要做的是通过电话号码搜索用户有/没有国家代码。

好吧,如果请求

“PHONE_NUMBERS”: “104152341”, “124254364”]

话,我想谁也完全匹配其所属的电话号码的用户请求具有/不具有国家代码的电话号码数组。

所以,我试着如下,但得到错误“无效的操作符'$''。

User.aggregate(
    [ 
     { "$redact": { 
      "$cond": [ 
       { 
        "$in": [ { "$concat": [ "$phone_number.country_code", "$phone_number.local_number" ] }, req.body.phone_numbers] 
       }, 
       "$$KEEP", 
       "$$PRUNE" 
      ] 
     }} 
    ], 
    function(err, users) { 
     // Do something 
     if (err) { 
      return res.json({ success: false, err: err }); 
     } 
     res.json({ success: true, users: users }); 
    } 
) 

我希望知道如何处理我的问题。

请帮助我!

回答

0

使用$setIsSubset作为你的条件表达式:

{ "$redact": { 
    "$cond": [ 
     { 
      "$setIsSubset": [ 
       [ 
        { 
         "$concat": [ 
          "$phone_number.country_code", 
          "$phone_number.local_number" 
         ] 
        } 
       ], 
       req.body.phone_numbers 
      ] 
     }, 
     "$$KEEP", 
     "$$PRUNE" 
    ] 
}} 
+1

优秀的,但我们也需要处理有/无国家代码,$ setIsSubset好两个字段的串联。 :) – BigDaddy