【发布时间】:2022-10-24 06:35:30
【问题描述】:
我搜索猫鼬方法将我的新字段添加到现有集合中,我尝试了 findByIdAndUpdate() 的功能,但它不起作用,还有其他方法吗?请告诉我!
这是我的架构代码:
const mongoose=require('mongoose');
const Schema=mongoose.Schema;
const AccountSchema=new Schema({
account:{
type:String,
required:true
},
password:{
type:String,
required:true
},
strick:false
});
const Account=mongoose.model('Account',AccountSchema);
module.exports=Account;
和应用代码:
app.post('/api/sendToShoppingCart',async(req,res)=>{
const db=await mongoose.connect("private");
const instance=await Account.findByIdAndUpdate(req.body.memberID,{
shoppingCart:Array //new field
});
console.log(instance);//it find it
await instance.save(); //save update
db.disconnect();//clsoe mongodb
});
谢谢你的帮助!
【问题讨论】:
-
当前代码有什么问题?
-
@KonradLinkowski,我的 Mongodb 云没有增加新的字段,我不知道为什么它不能工作!
-
为什么要传递数组构造函数而不是数组
[]?