Insert data into an array using mongodb?

Ok so basically, whenever there is no data I make data by doing:

image

I want it so that if the data does exist it makes a new character in the array characters in a JSON format. I want it so that it does this but it finds the data by user ID. So something like:

!newcharacter characterimage charactername

and it pushes all this into a new json object in the array characters

Hi there @HK420
Right, I have tried to gather what I can from your post, so let me get this straight. You make a character if there is no data. Data in what? The characters array? So, you find data using:

Data.findOne({_id: theid}, (err, datafound)=>{
})

assuming you are using mongoose.
Then what do you want to do with that data? What do you want to do if data is found? Push to an array. What array? Is it in that Data that is found? Do you have schemas for where you want the data to go?

No, I don’t make a character if there is no data. I make the whole model when there’s no data for the user. For this specific command I want to make character data so that’s what I did. What I want to do is that whenever the command is executed, it makes a new JSON object in the array of characters.

An example would be:

[{
                        name: something,
                        images: [`something else`],
                        upvotes: 0,
                        downvotes: 0,
                        status: "Private",
                        CharacterID: character ID
                    }],

This data would be made in characters if there is no data that the user has.

I want it so if the user already has data, it makes another JSON object like:

[{
                        name: something,
                        images: [`something else`],
                        upvotes: 0,
                        downvotes: 0,
                        status: "Private",
                        CharacterID: character ID
                    }, {
                        name: something1,
                        images: [`something else1`],
                        upvotes: 0,
                        downvotes: 0,
                        status: "Private",
                        CharacterID: character ID1
                    }],

But to do this I have to insert data into the characters array

Hi again @HK420!
Oh I see now. Right, so all you need to do is use the push function of findOneAndUpdate()
e.g.

Users.findOneAndUpdate({userID: userid, GuildID: guildid}, {$push: { characters:{name: name, images:image, upvotes:0, downvotes:0, status: "Private", CharacterID uuidv4() }}}, (err, doc)=>{
message.channel.send("Done!")
})

or

var characterobj = {
name: name,
images:image,
upvotes:0,
downvotes:0,
status: "Private",
CharacterID uuidv4()
}
Users.findOneAndUpdate({userID: userid, GuildID: guildid}, {$push: { characters:characterobj}}, (err, doc)=>{
message.channel.send("Done!")
})

Hope this helps! :slight_smile:

1 Like

Oh wow! Thanks ill try this out

Let me know if it works! :slight_smile:

2 Likes

Everything worked perfectly! Im just getting this in the console for some reason:

(node:14792) DeprecationWarning: Mongoose: `findOneAndUpdate()` and `findOneAndDelete()` without the `useFindAndModify` option set to false are deprecated. See: https://mongoosejs.com/docs/deprecations.html#findandmodify

I think it’s just how my mongodb configuration is done.

mongoose.set('useFindAndModify', false);
2 Likes

That’s what I did!

Welcome back HK420! In MongoDB, I believe you have to pass settings like
{useFindAndModify: true} in the connection argument. However, you’re using Mongoose, which might be different…

1 Like