Reputation: 1069
my mongoose schema is like this.
var ProfileAddressSchema = new Schema({
streetAddress: { type: String, required: true },
businessAddress: { type: String, required: false },
country: {type: Schema.Types.ObjectId, ref: 'Country', required: false },
city: { type: Schema.Types.ObjectId, ref: 'City', required: false },
province: { type: String, required: false },
postalCode: { type: String, required: false },
isDelete: { type: Boolean, default: false },
sequence: { type: Number, default: 0 },
isDefault: { type: Boolean, default: false }
});
var ProfileInfoSchema = new Schema({
name: { type: String, required: true },
firstName: { type: String, required: false },
addressList:[ProfileAddressSchema],
});
I have tried as follows and that does not works. I want to get the profiles that match with the provided postal code.
var criteria = {};
criteria.addressList = {}
criteria.addressList.postalCode = new RegExp(searchPrameters.postalCode, "i"); }
serviceHelper.queryModel(ProfileInfo,criteria, { __v: false }, callback);
Upvotes: 2
Views: 1887
Reputation: 103305
Use the dot notation to query embedded documents. Your query as a result should be like
ProfileInfo.find({
"addressList.postalCode": new RegExp(searchPrameters.postalCode, "i")
}).exec(callback);
So you can construct the query object that has the dot notation key using the bracket notation:
var criteria = {};
criteria["addressList.postalCode"] = new RegExp(searchPrameters.postalCode, "i");
serviceHelper.queryModel(ProfileInfo,criteria, { __v: false }, callback);
For more examples, refer to the manual on the section Query an Array of Embedded Documents.
Upvotes: 4