MongoDB how to find documents by searching a Mixed Type field?

Viewed 308

I am using the mongoose, and have a search text like Guo. I have following mongo document:

I want to find the text in the field "info.value"

{
    "_id" : ObjectId("5aeefc3ce104b16c7e3337a2"),
    ...
    "info" : [ 
        {
            "availbleNotInOrgs" : [],
            "_id" : ObjectId("5b9e56dc09573f260c76af96"),
            "field" : ObjectId("5b73d31ecebabf4b0d07a44c"),
            "value" : "Guo"
        }, 
        {
            "availbleNotInOrgs" : [],
            "_id" : ObjectId("5b9e56dc09573f260c76af94"),
            "field" : ObjectId("5b73d3a7cebabf4b0d07a48e"),
            "value" : {
                "id" : "a0",
                "value" : "男"
            }
        }, 
        {
            "availbleNotInOrgs" : [],
            "_id" : ObjectId("5b9e56dc09573f260c76af92"),
            "field" : ObjectId("5b73d488cebabf4b0d07a4f9"),
            "value" : [ 
                7, 
                23
            ]
        }, 
        {
            "availbleNotInOrgs" : [],
            "_id" : ObjectId("5b9e56dc09573f260c76af90"),
            "field" : ObjectId("5b73d50acebabf4b0d07a548"),
            "value" : [ 
                {
                    "country" : "中国",
                    "code" : "+86",
                    "phoneNumber" : "15699511079"
                }, 
                {
                    "country" : "中国",
                    "code" : "+86",
                    "phoneNumber" : "1841234123",
                    "placeholder" : "130xxxxxxxx"
                }
            ]
        }
     ]
}

The schema looks like:

const NewUserSchema = new Schema({
  wechat: {
    unionid: { type: String },
    username: { type: String },
    ...
  },
  info: [{
    field: { type: mongoose.Schema.Types.ObjectId, ref: 'field' },
    value: Schema.Types.Mixed,
    availbleNotInOrgs: [{ type: mongoose.Schema.Types.ObjectId, ref: 'organization' }]
  }],
});

The type of "info.value" is Schema.Types.Mixed so that value could be an array, an object, a string...

For instance, I expect to match the following documents that info.value contains Guo

{
    "info" : [ 
        {
            "value" : [ 
                {
                    "country" : "Guo",
                    "phoneNumber" : "15699511079"
                }, 
                {
                    "code" : "+86",
                    "phoneNumber" : "15699511079"
                }
            ]
        }
     ]
}
{
    "info" : [ 
        {
            "value" : [ 
                {
                    "code" : "+86",
                    "users" : ["15699511079","Guo"]
                }
            ]
        }
     ]
}

What can I do if I keep the type of "info.value"?

0 Answers
Related