How to save an array of objects into a mongoose db

20
May 16, 2019, at 00:00 AM

I am trying to save a leader board made of objects nested in an array. I want to save it in my database, but I have not been able to create the right schema and I don't think I that is the best way to go. When I run the code I get the error of:

"LeaderBoardSchema is not a constructor".

What is the appropriate way of creating a schema that I need.

I have tried many variations looking online, but I keep getting the " LeaderBoardSchema is not a constructor".

The examples from other questions on S.O have not been able to help me much.

  
  
 // leaderboard object that i want to save 
 leaderBoard = [ 
        leaderOne=   { 
            name: 'Ultimate Beast', 
            score: 2 
        }, 
        leaderTwo=  { 
            name: 'Masked Titan', 
            score: 9 
        }, 
        leaderThree=  { 
            name: 'Oranolio', 
            score: 7 
        }, 
        leaderFour=  { 
            name: 'Popularkiya', 
            score:1 
        }, 
        leaderFive=  { 
            name: 'Bootecia', 
            score: 11 
        }, 
    ]; 
 
 
 
 
// Database schema  
 
 
const Schema = mongoose.Schema() 
 
const LeaderBoardSchema = new mongoose.Schema({ 
   leaderBoard:{ 
    leaderOne :   { 
        name: String, 
        score: Number 
    }, 
    leaderTwo :  { 
        name: String, 
        score: Number 
    }, 
    leaderThree :  { 
        name: String, 
        score: Number 
    }, 
    leaderFour :  { 
        name: String, 
        score:Number 
    }, 
    leaderFive :  { 
        name: String, 
        score: Number 
    } 
   } 
  }, {collection: 'leaderboard-data'}); 
 
const PlayerData = mongoose.model('LeaderBoard Data', LeaderBoardSchema); 
 
 
 
// My attempt  
 
 
const leaderBoardToSave = new LeaderBoardSchema({ 
        leaderBoard:{ 
            leaderOne :   { 
                name: 'asdf', 
                score: 12 
            }, 
            leaderTwo :  { 
                name: 'sfgh', 
                score: 12 
            }, 
            leaderThree :  { 
                name: 'weh', 
                score: 12 
            }, 
            leaderFour :  { 
                name: 'asdf', 
                score:12 
            }, 
            leaderFive :  { 
                name: 'asdf', 
                score: 12 
            } 
        } 
    })

Answer 1

Currently your leaderBoard field is an object. To model an array of objects do the following with your schema:

const LeaderBoardSchema = new mongoose.Schema({
   leaderBoard: [
    {
        name: String,
        score: Number
    }
   ]
  }, {collection: 'leaderboard-data'});

as for the issue with the schema constructor. You're creating the mongoose model as follows const PlayerData = mongoose.model('LeaderBoard Data', LeaderBoardSchema);. But then you do new LeaderBoardSchema({...}). You need to use the mongoose model PlayerData instead. so to create a new leaderboard:

const leaderBoardToSave = new PlayerData({
        leaderBoard: [
           {
              name: 'asdf',
              score: 12
           },
           {
              name: 'gds',
              score: 12
           },
           {
              name: 'adad',
              score: 12
           },
       ]
    })
READ ALSO
How to insert a Distinct and Order in include?

How to insert a Distinct and Order in include?

i'm doing a project in nodejs (express and angular) and im finding difficulties with this sequielizeI cannot add a distinct (at the beginning of the query, i receive too much copies) and i cannot add an order by for a "include field"

18
multiple sockets on one client using angular

multiple sockets on one client using angular

I'm not even sure if this is possibleIf not, please point me in a different direction

25
Meaning in nodejs/postgreSQL : query.query2+='DELETE FROM public.category_qb_map WHERE qb_id=${id};';

Meaning in nodejs/postgreSQL : query.query2+='DELETE FROM public.category_qb_map WHERE qb_id=${id};';

I am writing an api in nodejs, in which some data are taken from the body of the request and insert, update and delete operations are performedI am having difficulty in understanding some portions of the code

28
Get a variable from a file to another file in node.js( problem not solve )

Get a variable from a file to another file in node.js( problem not solve )

I tried too many time but I couldn't a get solution to my problemmy file structure is like following:

12