Mongodb findOneAndUpdate不';不创建ObjectId
我需要发出补丁请求,以便同时只更新一个(或多个)字段。 我有一个大对象,它是我的文档,在嵌套的对象数组中 例如,对于我的汽车阵列,以下是模式:Mongodb findOneAndUpdate不';不创建ObjectId,mongodb,express,mongoose,mongoose-schema,Mongodb,Express,Mongoose,Mongoose Schema,我需要发出补丁请求,以便同时只更新一个(或多个)字段。 我有一个大对象,它是我的文档,在嵌套的对象数组中 例如,对于我的汽车阵列,以下是模式: const carSchema = new Schema({ owner: [{value: {type: String}, label: {type: String}}], carPlate: {type: String}, carColor: {type: String}, carBrand: {type: String}, ca
const carSchema = new Schema({
owner: [{value: {type: String}, label: {type: String}}],
carPlate: {type: String},
carColor: {type: String},
carBrand: {type: String},
carStatus: {type: String}
});
const myObject = new Schema({
...
cars: [carSchema]
...
});
当我发送更改时,我会这样做:
让dynamicVar='cars.+i+'.+myfield;
this.props.updateGeneral({u-id:this.props.general.\u-id[dynamicVar]:[myfield.value]})
我在redux上,所以我的动作看起来像:
export function updateGeneral(data) {
let _id = data._id;
delete data._id;
return {
type: 'UPDATE_GENERAL',
payload: client.patch(`${url}/${_id}`, data)
}
}
我的补丁请求如下:
router.patch('/url/:id', async (req, res, next) => {
myObject.findOneAndUpdate({_id: req.params.id}, {$set: req.body }, {upsert: true, new: true}, function (err, objectReturn) {
if (err) return next(err);
cn = cn.substr(0, cn.indexOf(' {'));
res.json(objectReturn);
});
});
我最大的问题是我的字段被更新或插入,但如果它被插入并创建了一个新数组,它将不会创建objectId链接。它甚至不会创建对象数组,只是一个具有属性的对象
如何使mongoose启动ObjectId???每个回复看起来都无法更新对象ID。执行此操作时,您实际上是在“删除”对象并创建一个新对象。请更好地格式化您的帖子。无法使用mongodb ObjectId进行升级。。。objectId是自动生成的字段,不能在upsertion中使用