我试图使用$addToSet将字符串数组添加到MongoDB中。
对象设置如下所定义:
var mongoose =需要量(‘mongoose’);var Schema = mongoose.Schema;
module.exports = mongoose.model('Company', {
License: { type: String, index: true },
"Classifications": [
{
Classification: { type: String, _id: false }
}
]
});当前对象在Mongo中如下所示:
{
"_id": {
"$oid": "55b03f9337b46f10d38843d7"
},
"Classifications": [
"A - GENERAL ENGINEERING CONTRACTOR",
"B - GENERAL BUILDING CONTRACTOR",
"C12 - EARTHWORK AND PAVING"
],
"License": "CA_*****"
}我试图在以下数组上运行$addToSet:
var classifications = [ 'B - GENERAL BUILDING CONTRACTOR',
'C10 - ELECTRICAL',
'C33 - PAINTING AND DECORATING',
'C29 - MASONRY',
'C-9 - DRYWALL',
'C54 - TILE (CERAMIC AND MOSAIC)',
'C-5 - FRAMING AND ROUGH CARPENTRY',
'C20 - WARM-AIR HEATING, VENTILATING AND AIR-CONDITIONING',
'C36 - PLUMBING',
'C39 - ROOFING' ]
Company.update({ '_id': company._id }, { $addToSet: { "Classifications": { $each: classifications } } }当我在Node中运行Company.update时,我会得到错误
TypeError:无法使用“in”运算符搜索C39 - ROOFING中的“_id”
知道为什么会这样吗?
发布于 2015-09-14 22:03:06
终于解决了我的问题。
在模式中,我有:
"Classifications": [
{
Classification: { type: String, _id: false }
}
]正在寻找一个有字符串的物体。架构应该是:
"Classifications": [String]它允许
Company.update({ '_id': company._id }, { $addToSet: { "Classifications": { $each: classifications } } }在没有问题的情况下运行并添加数组中的字符串
https://stackoverflow.com/questions/32573062
复制相似问题