gpt4 book ai didi

node.js - 这个 mongoose 部分更新有什么不正确的地方

转载 作者:太空宇宙 更新时间:2023-11-04 01:08:06 25 4
gpt4 key购买 nike

我有以下称为场景的架构

var scenarios = new Schema({
title: 'String',
type: 'String',
description: 'String',
authorId: 'String',
presentation: [presentations_schema],
revision: 'String',
createDate: 'String',
updateDate: 'Date',
active: 'Boolean',
display: 'Boolean',
video: [video_schema],
});

我正在尝试更新演示文稿架构中的字段

var scenario_presentations = new Schema({
scenarioId: 'String',
pageLocation: 'String',
pageNumber: [Number],
syncManifest: [Number],
caption:'String',
createdDate: 'Date',
updateDate: 'Date',
active: 'Boolean',
display: 'Boolean'
});

我尝试根据此处的答案运行更新 Partial update of a subdocument with nodejs/mongoose

这是我的尝试

var scenarios = require('../models/scenarios').Scenarios;
exports.updateScenario = function (req,res){

scenarios.update({_id: '52cd8f43c1e8ba5009000008', presentation_id: '52cd8f43c1e8ba5009000009'},
{$set: { 'presentation.$.pageLocation': 'someUrl'}},
function(err, numberAffected, rawResponse) {
if (err) {
console.log(err);
console.log('error returned');
res.send(500, { error: 'Failed insert'});
}

if (!numberAffected) {
console.log(rawResponse);
res.send(403, { error: 'No rows affected' });
}
res.send(200);
});

我不断收到if (!numberAffected) = true这是我尝试更新的文档

{
authorId: "Austin(ShouldBeAHash)",
revision: "1",
active: true,
display: true,
sortOrder: 0,
title: "sdfg",
description: "gfds",
_id: ObjectId("52cd8f43c1e8ba5009000008"),
bundleId: [],
video: [
{
thumbnailLocation: "someUrl",
videoLocation: "SomeUrl",
_id: ObjectId("52cd8f43c1e8ba500900000a")
}
],
status: [],
presentation: [
{
pageLocation: "",
_id: ObjectId("52cd8f43c1e8ba5009000009"),
syncManifest: [
0
],
pageNumber: [
0
]
}
],
subcategories: [],
categories: [],
__v: 0
}

最佳答案

update 调用的选择器参数中,presentation_id 应为 'presentation._id':

scenarios.update({
_id: '52cd8f43c1e8ba5009000008',
'presentation._id': '52cd8f43c1e8ba5009000009 }, ...

关于node.js - 这个 mongoose 部分更新有什么不正确的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21005111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com