Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

query relation data in mongoose

var UserSchema = new mongoose.Schema({
    username: {
        type: String,
        index: true,
        required: true,
        unique: true
    },
    email: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    scores: [{ type: Schema.Types.ObjectId, ref: 'Score' }]
});

var GameSchema = new mongoose.Schema({
    name: String
});

var LobbySchema = new mongoose.Schema({
    _game: { 
       type: mongoose.Schema.Types.ObjectId, 
       ref: 'Game' 
     },
    name: String
});

var ScoreSchema = new mongoose.Schema({
    _user : { 
         type: mongoose.Schema.Types.ObjectId, 
         ref: 'User' 
       },
    _game : { 
         type: mongoose.Schema.Types.ObjectId, 
         ref: 'Game' 
       },
    score: Number
});


ScoreSchema.find({_id:##userIdHere##})
           .populate('_user')
           .populate('_game')  
           .exec(function(err, foundScore){
                   if(err){
                      res.send(err)
                    } else {
                 res.send(foundScore)
   }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: sql result to javascript array 
Javascript :: tailwind only dropdown 
Javascript :: javascript change get parameter without reload 
Javascript :: jquery ui dialog live cdn 
Javascript :: routing in react 
Javascript :: javascript nested objects 
Javascript :: MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms 
Javascript :: ngfor with different id 
Javascript :: jest visit a page 
Javascript :: working with binary and base64 data 
Javascript :: find only vowels in string Javascript 
Javascript :: london turnbridgewells 
Javascript :: nodejs validate bnb wallet address 
Javascript :: javascript generator send vs next 
Javascript :: scratch addons 
Javascript :: why my expo token change each time 
Javascript :: ex:javascript array 
Javascript :: react prototype function 
Javascript :: node-google-spreadsheet color border 
Javascript :: javascript short syntax get element 
Javascript :: model to js 
Javascript :: processing map in javascript 
Javascript :: javascript Bingo add called number to a list 
Javascript :: dynamic select paragraph id using javascript 
Javascript :: making js local function globally accessible 
Javascript :: how to add set between two date in datatable 
Javascript :: image continuous changing div react 
Javascript :: d3js circle out of scrren 
Javascript :: store current date in chrome storage extension 
Javascript :: cannot find name json angular 7 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =