Reputation: 97
I have trouble understanding the syntax of updateOne and insertOne. I know if I don't have a field I can use updateOne and use $set to create a field. But I have an example here
const listSchema=new mongoose.Schema({
name:String,
items:Array
});
const List=mongoose.model("List",listSchema);
const list =new List({
name: "fruits",
items:[]
});
so essentially I already made the field, but I would like to add items into list.items array, so in this case do I use update or insert? And can you tell me some general knowledge about when to use insert and when to use update? Thank you!
Upvotes: 0
Views: 34
Reputation: 22956
You need to learn about CRUD generally.
Insert: Nothing exists, but you need to create them.
Update: exists something, you need to add/modify/remove something.
Let's say, here you have empty items
. If you have to add items, it falls under update category. There are many ways to update items in mongo[update
, updateOne
, updateMany
, set-update(aggregate)
]
Refer this to push item using mongoose
Upvotes: 1