admin管理员组

文章数量:1291062

I have a mongoose schema as follows

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

where reset is being given an object like this to store in the database

{
    id: 23,
    name: 'something'
}

I would like to look up a document based on the id in the reset object. This is what I have tried but I never get a result returned.

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

Is a lookup like this possible with mongoose?

I have a mongoose schema as follows

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

where reset is being given an object like this to store in the database

{
    id: 23,
    name: 'something'
}

I would like to look up a document based on the id in the reset object. This is what I have tried but I never get a result returned.

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

Is a lookup like this possible with mongoose?

Share Improve this question asked Nov 13, 2012 at 15:54 Errol FitzgeraldErrol Fitzgerald 3,0082 gold badges28 silver badges35 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

I think the issue you're having is with using mixed schema type.

Could you not use an embedded doc of Reset

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

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

And then querying like:

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

});

本文标签: javascriptMongoose findOne on object propertyStack Overflow