2016-10-21 3 views

答えて

1

populateを使用してリソースを保存しません。フードの下で、マングースは必要な回数だけデータベースを呼び出します。例を考えてみましょう:

Mongoose: accounts.find({}, { fields: undefined }) 
Mongoose: users.find({ _id: { '$in': [ ObjectId("5807d6d6aa66d7633a5d7025"), ObjectId("5807d6d6aa66d7633a5d7026"), ObjectId("5807d709aa66d7633a5d7027") ] } }, { fields: undefined }) 

マングースアカウント文書とし、それらの一つ一つについてユーザーを見つけることです:

別に結果から
module.exports = function(){ 
    var UserSchema = new Schema({ 
     email : {type : String, required: true}, 
     password: {type: String, required: true} 
    }); 
    return mongoose.model("User", UserSchema); 
}; 


module.exports = function(){ 
    var AccountSchema = new Schema({ 
     number : {type : String, required: true}, 
     user: {type: Schema.Types.ObjectId, ref: 'User'} 
    }); 
    return mongoose.model("Account", AccountSchema); 
}; 


mongoose.set('debug', true); //to see queries mongoose is using behind the scenes 
Account.find({}, function(err, res){ 
    console.log(res) 
}).populate("user") 

、コンソール上でこのように表示されます。

大量のコードを節約できるため、入力するフィールドの数に関係なく使用しない理由がわかりません。

関連する問題