Search code examples
javascriptmongodbstrapimongodb-atlas

How to store only the user id not whole user inside another document in strapi JS


From: The document struture like this

{
    name:"hello",
    user:[ 
        {_id:"namejkcnskcn1",username:"username1",name:"name1",...},
        {_id:"namejkcnskcn2",username:"username2",name:"name2",...},
        {_id:"namejkcnskcn3",username:"username3",name:"name3",...}
    ]
}

To this one to avoid waste of storage

{
    name:"hello",
    user:[ "namejkcnskcn","namejkcnskcn","namejkcnskcn"  ]//only ID's
}

Solution

  • Instead of making "user" as a array of object, you can make the "user" a simple array of id's.

    This is the schema

    users: [{
        type: Schema.ObjectId,
        ref: 'user'   }]
    

    Hope this will be helpfull.