Mongoose在字符串数组

时间:2018-01-22 13:34:56

标签: mongodb mongoose mongodb-query

考虑以下架构:

phone = {
   IMEI: String,
   Number: String 
}

customer = {
   name: String
   phones: [String]
}

以下数据:

$ db.customer.find({});

{
    "_id" : ObjectId("5a65d865b4957f25fc5e653a"),
    "name": "Greg Wilson",
    "phones": [
        {
            "IMEI": "356938035643809",
            "Number": "+1 718 283 4848"
        },
        {
            "IMEI": "356938035643810",
            "Number": "+1 718 283 4849"
        },
        {
            "IMEI": "356938035643811",
            "Number": "+1 718 283 4850"
        },
    ]
},
{
    "_id" : ObjectId("5a65d865b4957f25fc5e6575"),
    "name": "Bob Wilson",
    "phones": [
        {
            "IMEI": "356938035612302",
            "Number": "+1 718 111 1111"
        },
        {
            "IMEI": "356938035612303",
            "Number": "+1 718 111 1112"
        },
        {
            "IMEI": "3569380356612304",
            "Number": "+1 718 111 1113"
        },
    ]
}

我想找一个拥有手机的客户,其中号码包含特定的子串。这就是我尝试使用mongoose:

let textToSearch = '111';
let regex = new RegExp("^" + textToSearch);
let query = {};
let phones = {
     Number: regex
}
query.phones = phones;
Customers.find(query).sort({ name: 1 }).exec();

Mongoose没有返回任何数据。

搜索包含特定子字符串的电话号码的正确方法是什么(示例中为'111')?

0 个答案:

没有答案