简体   繁体   中英

Mongoose embedded document query returning null

I have the following schema :

_schema : {

Prize : new Schema({
        prizeName : { type : String },
        thumbnailImage : [ String ],
        detailImage : [ String ],
        prizeCategory : [ {type : String, index : true } ],
        prizeDescription : { type : String },
        prizePrice : { type : Number, required : true }
    }),

Game : new Schema ({
        roomName : { type : String, required : true },
        openTime : { type : Date },
        closeTime : { type : Date },
        minPlayers : { type : Number },
        maxPlayers : { type : Number, required : true },
        numberOfPlayers : { type : Number },
        winner : { userId : { type : ObjectId, index : true, ref : 'User'} },
        prize : [ this.Prize ],
        tag : [ { type : String, index : true } ],
        status : { type : Number, index : true },
        businessType : { type : Number, required : true, index : true },
        mallId : { type : ObjectId, ref : 'Mall' },
        registeredPlayers : { type : ObjectId, ref : 'User' } 
    }),

Schedule : new Schema ({
        _id : ObjectId,
        time : { type : Date, index : true },
        game : [ this.Game ]
    }),

}

However when I try to query the game embedded document the object is always null. I'm querying like so:

var Schedule = mongoose.model('Schedule', this._schema.Schedule);

Schedule.findById({'game._id' : req.params._id}).exec(function(err,gameDetail){...});

Am I doing anything wrong when I declare the schema and models? I have seen numerous examples where people appear to be doing exactly what I'm trying. Any help would be greatly appreciated! Thanks in advance.

A mongoose Model's findById method is used to find the instance of that Model with the _id that's supplied as the first parameter to the method. So Schedule.findById returns Schedule instances, not individual Game instances. Schedule.findOne({'game._id' : req.params._id}, ... will get you the Schedule instance containing the Game with that id, but if you need to query for Game instances by id, you should be keeping them in a separate collection instead of embedding them in Schedule .

Looking at your code, my first guess is actually that your findById isn't structured quite right.

First, you're using the value req.params._id to get the id. Most of the code examples I have seen, and my code, uses :id in the router ( app.get('/schedules/:id') ), which would actually mean the ID you're looking for is stored in req.params.id . You'll have to check that in your code.

Secondly, to my understanding, findById is only useful for finding, in this case, a Schedule by that ID. Try using just a normal find.

Last thought: you're missing a closing curly bracket at the end of {'game._id' : req.params._id} .

Hopefully something in that helps. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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