繁体   English   中英

对象属性上的Mongoose findOne

[英]Mongoose findOne on object property

我有一个mongoose架构如下

var user_schema = new Schema({
    reset : { type: Schema.Types.Mixed, required: true }
});

其中reset被赋予这样的对象以存储在数据库中

{
    id: 23,
    name: 'something'
}

我想根据重置对象中的id查找文档。 这是我尝试过但我从来没有得到结果返回。

models.Users.findOne({ 'reset.id': id }, function (err, user) {
    // user is null 
});

像mongoose这样的查找是否可行?

我认为你遇到的问题是使用混合模式类型。

你能不能使用Reset的嵌入式文档

var reset_schema = new Schema({
    id        : Int,
    name      : String
});

var user_schema = new Schema({
    name      : String,
    reset     : reset_schema 
});

然后查询如下:

models.Users.findOne({ 'reset.id': id }, function (err, user) {

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM