Javascript 根据文档中数组元素的值和文档本身的_id更新文档
如问题所述,我无法使此更新操作正常工作 我的设想: 我有一个Javascript 根据文档中数组元素的值和文档本身的_id更新文档,javascript,node.js,mongoose,Javascript,Node.js,Mongoose,如问题所述,我无法使此更新操作正常工作 我的设想: 我有一个事件、一个票证和一个票证采购。事件和TicketPurchase具有Ticket数组作为属性 我需要达到的目标是: 在TicketPurchase的票证数组中,更新特定票证的validated属性,从true/false开始 将同一票据的total_quantity属性从1递减。在主事件表中。(票证采购中的所有票证都是主事件表中票证的副本) 作为程序员,我几乎所有的经验都花在了MySQL上,所以我仍然是NoSQL世界的初学者 我所尝试的
事件
、一个票证
和一个票证采购
。事件
和TicketPurchase
具有Ticket
数组作为属性
我需要达到的目标是:
TicketPurchase
的票证数组中,更新特定票证的validated
属性,从true/false开始
total_quantity
属性从1递减。在主事件
表中。(票证采购中的所有票证都是主事件
表中票证的副本)id
和\u id
的用法,将$set
等运算符放入和移出“”标记,以及所有其他类似配置,没有任何结果const TicketSchema = new Schema({
type : {type: String},
total_quantity : {type: Number},
price : {type: String},
limit_per_order: {type: Number},
start_date: {type: Date},
end_date: {type: Date},
description: {type: String},
validated: {type: String, default: 'false'}
});
const EventSchema = new Schema({
title: {type: String},
location: {type: String},
start_date: {type: Date},
start_time: {type: String},
end_date: {type: Date},
end_time: {type: String},
description: {type: String},
organizer_name: {type: String},
organizer_about: {type: String},
cover_picture: {type: String},
tickets: [{type: Schema.Types.ObjectId, ref: 'Ticket'}],
access_code: {type: String, default: shortid.generate}
});
const TicketPurchaseSchema = new Schema({
user: {type: Schema.Types.ObjectId, ref: 'User'},
event: {type: Schema.Types.ObjectId, ref: 'Event'},
tickets: [{type: Schema.Types.ObjectId, ref: 'Ticket'}],
time_stamp: {type: Date}
});
for(var x of ticketPurchase.tickets){
//console.log(x);
if(x.id === ticket_id && x.validated === 'false'){
console.log('ticket not validated. Update');
TicketPurchase.update(
{_id: ticket_purchase_id, 'tickets._id': ticket_id},
{'$set':{
'tickets.$.validated': 'true'
}},
function (err){
console.log('updated validated');
if(err){console.log(err);}
}
);
Event
.update({_id: event_id, "tickets._id": x.id},
{$inc : {"tickets.$.total_quantity" : -1}});
console.log('updated ticket.total_qty');
payload['success'] = 'true';
}else if(x.id === ticket_id && x.validated === 'true'){
console.log('ticket validated');
payload['success'] = 'false';
payload['message'] = 'Ticket already validated.';
}
}
event.js
const TicketSchema = new Schema({
type : {type: String},
total_quantity : {type: Number},
price : {type: String},
limit_per_order: {type: Number},
start_date: {type: Date},
end_date: {type: Date},
description: {type: String},
validated: {type: String, default: 'false'}
});
const EventSchema = new Schema({
title: {type: String},
location: {type: String},
start_date: {type: Date},
start_time: {type: String},
end_date: {type: Date},
end_time: {type: String},
description: {type: String},
organizer_name: {type: String},
organizer_about: {type: String},
cover_picture: {type: String},
tickets: [{type: Schema.Types.ObjectId, ref: 'Ticket'}],
access_code: {type: String, default: shortid.generate}
});
const TicketPurchaseSchema = new Schema({
user: {type: Schema.Types.ObjectId, ref: 'User'},
event: {type: Schema.Types.ObjectId, ref: 'Event'},
tickets: [{type: Schema.Types.ObjectId, ref: 'Ticket'}],
time_stamp: {type: Date}
});
for(var x of ticketPurchase.tickets){
//console.log(x);
if(x.id === ticket_id && x.validated === 'false'){
console.log('ticket not validated. Update');
TicketPurchase.update(
{_id: ticket_purchase_id, 'tickets._id': ticket_id},
{'$set':{
'tickets.$.validated': 'true'
}},
function (err){
console.log('updated validated');
if(err){console.log(err);}
}
);
Event
.update({_id: event_id, "tickets._id": x.id},
{$inc : {"tickets.$.total_quantity" : -1}});
console.log('updated ticket.total_qty');
payload['success'] = 'true';
}else if(x.id === ticket_id && x.validated === 'true'){
console.log('ticket validated');
payload['success'] = 'false';
payload['message'] = 'Ticket already validated.';
}
}
ticketPurchase.js
const TicketSchema = new Schema({
type : {type: String},
total_quantity : {type: Number},
price : {type: String},
limit_per_order: {type: Number},
start_date: {type: Date},
end_date: {type: Date},
description: {type: String},
validated: {type: String, default: 'false'}
});
const EventSchema = new Schema({
title: {type: String},
location: {type: String},
start_date: {type: Date},
start_time: {type: String},
end_date: {type: Date},
end_time: {type: String},
description: {type: String},
organizer_name: {type: String},
organizer_about: {type: String},
cover_picture: {type: String},
tickets: [{type: Schema.Types.ObjectId, ref: 'Ticket'}],
access_code: {type: String, default: shortid.generate}
});
const TicketPurchaseSchema = new Schema({
user: {type: Schema.Types.ObjectId, ref: 'User'},
event: {type: Schema.Types.ObjectId, ref: 'Event'},
tickets: [{type: Schema.Types.ObjectId, ref: 'Ticket'}],
time_stamp: {type: Date}
});
for(var x of ticketPurchase.tickets){
//console.log(x);
if(x.id === ticket_id && x.validated === 'false'){
console.log('ticket not validated. Update');
TicketPurchase.update(
{_id: ticket_purchase_id, 'tickets._id': ticket_id},
{'$set':{
'tickets.$.validated': 'true'
}},
function (err){
console.log('updated validated');
if(err){console.log(err);}
}
);
Event
.update({_id: event_id, "tickets._id": x.id},
{$inc : {"tickets.$.total_quantity" : -1}});
console.log('updated ticket.total_qty');
payload['success'] = 'true';
}else if(x.id === ticket_id && x.validated === 'true'){
console.log('ticket validated');
payload['success'] = 'false';
payload['message'] = 'Ticket already validated.';
}
}
update.js
const TicketSchema = new Schema({
type : {type: String},
total_quantity : {type: Number},
price : {type: String},
limit_per_order: {type: Number},
start_date: {type: Date},
end_date: {type: Date},
description: {type: String},
validated: {type: String, default: 'false'}
});
const EventSchema = new Schema({
title: {type: String},
location: {type: String},
start_date: {type: Date},
start_time: {type: String},
end_date: {type: Date},
end_time: {type: String},
description: {type: String},
organizer_name: {type: String},
organizer_about: {type: String},
cover_picture: {type: String},
tickets: [{type: Schema.Types.ObjectId, ref: 'Ticket'}],
access_code: {type: String, default: shortid.generate}
});
const TicketPurchaseSchema = new Schema({
user: {type: Schema.Types.ObjectId, ref: 'User'},
event: {type: Schema.Types.ObjectId, ref: 'Event'},
tickets: [{type: Schema.Types.ObjectId, ref: 'Ticket'}],
time_stamp: {type: Date}
});
for(var x of ticketPurchase.tickets){
//console.log(x);
if(x.id === ticket_id && x.validated === 'false'){
console.log('ticket not validated. Update');
TicketPurchase.update(
{_id: ticket_purchase_id, 'tickets._id': ticket_id},
{'$set':{
'tickets.$.validated': 'true'
}},
function (err){
console.log('updated validated');
if(err){console.log(err);}
}
);
Event
.update({_id: event_id, "tickets._id": x.id},
{$inc : {"tickets.$.total_quantity" : -1}});
console.log('updated ticket.total_qty');
payload['success'] = 'true';
}else if(x.id === ticket_id && x.validated === 'true'){
console.log('ticket validated');
payload['success'] = 'false';
payload['message'] = 'Ticket already validated.';
}
}
首先,这里是一个简短的示例,说明了如何避免循环,并仅使用mongoose方法完成所有操作:
TicketPurchase.findById(purchaseId, (err, ticketPurchase) => {
Ticket.update({ _id: { $in: ticketPurchase.tickets }, _id: ticket_id, validated: 'false' },
{ $set: { validated: 'true' }, $inc: { total_quantity: -1 } },
(err, outcome) => {
console.log(`The number of validated tickets for the Purchase #${purchaseId} is ${outcome.nModified}`);
});
});
在这里,它在相同的操作中执行所有操作:查找属于购买且尚未验证的票证,然后将已验证属性设置为“true”,并递减数量。最后,您将获得修改记录的总数
在您的示例中,我看不到您在purchaseTicket中填充了您的票证。要以您编写的方式迭代票证对象,您必须为其父实体填充它,因为属性ticketPurchase.tickets
只包含引用。表达式如下所示:TicketPurchase.findById(purchaseId).populate('tickets')。然后(purchase=>{…})
其中,purchase
将属性tickets
填充为其ticket对象,而不仅仅是id对象
除此之外,您似乎不必在事件模式中更新票证,因为您可以直接在票证模式中进行更新(就像我在我的示例开销中所做的那样),因为所有引用最终都指向模式中的对象。谢谢您的简洁解释和代码,先生。