node.js - 如何将数组添加到存在于 mongodb 对象中的另一个数组中
问题描述
这是我的架构
const CustomerSchema = mongoose.Schema(
{
name: {
type: String,
trim: true,
},
purchased: {
modelsId: [{
type: mongoose.Schema.ObjectId ,
trim: true,
}],
collectionsId: [{
type: mongoose.Schema.ObjectId,
trim: true,
}]
}
},
{ timestamps: true }
);
我想用猫鼬将一个数组添加到购买对象中的modelsId数组中
Customer.findOneAndUpdate({ _id: customer._id }, { $addToSet: { 'purcased.modelsId': { $each: modelsId } } },
{ new: true }).then(res => {
console.log('response', res)
}).catch(e => {
console.log('error', e)
})
它不工作!感谢您的帮助!
解决方案
看起来你可能只是有一个简单的拼写错误?尝试更改'purcased.modelsId'
为'purchased.modelsId'
. 我想你只是少了一个“h”!
推荐阅读
- sql - select count(*) 导致 LEFT OUTER JOIN 在切换表后是相同的,我不明白为什么
- css - Vue.js 范围样式不适用于单个组件
- input - Vue.js - 如何在输入更改时更新表单组标签图标类
- python - 通过 Python 中出现的所有非字母字符分割字符串
- python - 如何修复此代码的错误:“dirac[:N / 2] = 1”?
- python - 获取错误 - year = int(year_field.get()) ValueError: invalid literal for int() with base 10: ''
- angular - 从 ID 数组中检索数据 firebase (Angular)
- c++ - 计算光线和网格之间的交点
- java - 线程“主”java.lang.AbstractMethodError 中的异常:接收器类 org.openqa.selenium.chrome.ChromeOptions 未使用 Selenium 定义错误
- java - 将 H2 数据库与 Spring Boot 集成