gpt4 book ai didi

node.js - 通过嵌套文档属性进行 Mongoose 查询

转载 作者:太空宇宙 更新时间:2023-11-03 23:03:11 25 4
gpt4 key购买 nike

我的 Mongoose 模式是这样的。

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],

});

我已经尝试了以下方法,但不起作用。我想要获取与所提供的邮政编码匹配的个人资料。

var criteria = {};
criteria.addressList = {}
criteria.addressList.postalCode = new RegExp(searchPrameters.postalCode, "i"); }
serviceHelper.queryModel(ProfileInfo,criteria, { __v: false }, callback);

最佳答案

使用dot notation查询嵌入文档。您的查询结果应该类似于

ProfileInfo.find({
"addressList.postalCode": new RegExp(searchPrameters.postalCode, "i")
}).exec(callback);

因此您可以构造具有 dot notation 的查询对象使用bracket notation键:

var criteria = {};
criteria["addressList.postalCode"] = new RegExp(searchPrameters.postalCode, "i");
serviceHelper.queryModel(ProfileInfo,criteria, { __v: false }, callback);

有关更多示例,请参阅手册 Query an Array of Embedded Documents 部分.

关于node.js - 通过嵌套文档属性进行 Mongoose 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42344735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com