Hello!
Teach me, my lords, how to put information into a deeply nested object in the database. I tryed everything!
This my schema:
const server = new Schema({
name: String,
icon: String,
owner: String,
settings: Array,
users: Array,
textChannels: Array,
voiceChannels: Array
})
This code inst work:
const {channelName} = data;
const message = {text, date, username};
await serverModel.updateOne({name: serverName}, {
$push: {
[`textChannels.${channelName}.messages`]: {message}
}
});
This code create duplicate channel every time:
const {channelName} = data;
const message = {text, date, username};
await serverModel.updateOne({name: serverName}, {
$push: {
textChannels: {
[channelName]: {
messages: {message}
}
}
}
});
Like this:
I just want to insert a message
object into a messages
object. I want all the messages were in one place! I want to be the lord of messages! Help me, please!
And another one question: how do I get rid of the null object (underlined in red) and make the nesting “clean” like internal objects?