如何删除 Meteor collection 中数组中的某个字段?
How to delete a certain field in an array inside Meteor collection?
我无法在 Meteor collection 中删除数组中的字段。这是我的 collection:
的结构
我试过这个:
SMUProfiles.update({
owner: this.userId,
'classrooms.owner': classroom_id,
}, {
$pull: {
'classroom.$.owner': classroom_id
}
}
)
但没有成功。
我想删除 'owner' 数组下的任何键,我只引用它的值,而不是它们的索引。在这种情况下,我将 6Yi64LqpqnfsHv4ms
引用为 classroom_id
.
显然,目前没有简单的方法可以做到这一点。因此,为了其他可能正在寻找类似问题的人的利益,这是我的解决方案:
//** Method for deleting Classroom */
'classroom.delete'(classroom_id){
if(!this.userId){
throw new Meteor.Error('not-authorised');
}
Classrooms.remove(classroom_id)
let classids = Classrooms.find({ owner: this.userId
}).fetch().map(function(classrooms){
return classrooms._id })
//console.log(classids);
SMUProfiles.update({
owner: this.userId,
}, {
$set: {
'classrooms.owner': classids
}
}
)
},
这个解决方案背后的故事:
我有 2 个集合 Classrooms
和 SMUProfiles
。注册后,将在该用户的 SMUProfile 下创建一条新记录,其中包含一些其他详细信息(请参阅上面问题中的图片),包括用户创建的教室 ID 的记录。
现在,当用户删除 Classrooms
集合中的教室时,我还需要删除 SMUProfile
中已删除的教室的 ID。我最初试图从 SMUProfile
中的 id 数组中删除 id 但没有成功。
我通过在删除一个教室后重新读取 Classrooms
集合解决了这个问题,重新迭代数组中剩余教室的 ID,以及 $set
新的教室数组ID 为 SMUProfile
.
你试过了吗
SMUProfiles.update({
owner: this.userId,
}, {
$pull: {
'classrooms.owner': classroom_id
}
}
)
我认为带有对象路径的简单拉操作符应该足以从数组中拉取元素。
我无法在 Meteor collection 中删除数组中的字段。这是我的 collection:
的结构我试过这个:
SMUProfiles.update({
owner: this.userId,
'classrooms.owner': classroom_id,
}, {
$pull: {
'classroom.$.owner': classroom_id
}
}
)
但没有成功。
我想删除 'owner' 数组下的任何键,我只引用它的值,而不是它们的索引。在这种情况下,我将 6Yi64LqpqnfsHv4ms
引用为 classroom_id
.
显然,目前没有简单的方法可以做到这一点。因此,为了其他可能正在寻找类似问题的人的利益,这是我的解决方案:
//** Method for deleting Classroom */
'classroom.delete'(classroom_id){
if(!this.userId){
throw new Meteor.Error('not-authorised');
}
Classrooms.remove(classroom_id)
let classids = Classrooms.find({ owner: this.userId
}).fetch().map(function(classrooms){
return classrooms._id })
//console.log(classids);
SMUProfiles.update({
owner: this.userId,
}, {
$set: {
'classrooms.owner': classids
}
}
)
},
这个解决方案背后的故事:
我有 2 个集合 Classrooms
和 SMUProfiles
。注册后,将在该用户的 SMUProfile 下创建一条新记录,其中包含一些其他详细信息(请参阅上面问题中的图片),包括用户创建的教室 ID 的记录。
现在,当用户删除 Classrooms
集合中的教室时,我还需要删除 SMUProfile
中已删除的教室的 ID。我最初试图从 SMUProfile
中的 id 数组中删除 id 但没有成功。
我通过在删除一个教室后重新读取 Classrooms
集合解决了这个问题,重新迭代数组中剩余教室的 ID,以及 $set
新的教室数组ID 为 SMUProfile
.
你试过了吗
SMUProfiles.update({
owner: this.userId,
}, {
$pull: {
'classrooms.owner': classroom_id
}
}
)
我认为带有对象路径的简单拉操作符应该足以从数组中拉取元素。