为什么在 MongoDB updateOne 中使用切片不会从数组中删除项目,而只是用一个奇怪的对象替换它?
所以有数组课程如下 例如,在它的内部有这样的对象:
objInside = {
username:"SweetWhite"
courseTitle:"TOMATOaaaaa"
progress:0
}
我有一个函数可以在该类对象的对象数组中找到正确的对象索引 它似乎在找到的索引和对象方面起作用,但是当我执行它时,我的更新请求工作得很奇怪:
User.updateOne({username: req.body.username}, {coursesFollowing:{$slice: [removeIndex , 1]}}, function(err,result){
if(err){return next(err)}
})
它找到了正确的用户将其放入,以及正确的字段,但是它不会删除该 < 中的对象code>removeIndex 索引,但删除数组中的所有项目并仅放入一个对象,数组名为 $slice,其中 removeIndex
作为第一个值,第二个值为 1,从数据来看还没有删除所有其他对象,但只是将它们替换为自身,操作 $slice 将其自身推入数组并且我认为没有执行?我有点困惑。
无论如何,我该如何解决这个问题?如何删除数组的索引而不推送奇怪的 $slice 对象数组?
谢谢!
so there the array coursesFollowing
and inside it objects like this for example:
objInside = {
username:"SweetWhite"
courseTitle:"TOMATOaaaaa"
progress:0
}
and I have a function that finds the right object index in the array of objects of that kind of object
and it seems to work in terms of index found and object, however my update request works weirdly when I execute it:
User.updateOne({username: req.body.username}, {coursesFollowing:{$slice: [removeIndex , 1]}}, function(err,result){
if(err){return next(err)}
})
it find the right user to put it in, and also the right field, however it does not remove the object in that removeIndex
index, but deletes all the items in the array and puts only one object in, with the array named $slice with the removeIndex
as first value and the second value is 1, judging from the data it has not deleted all the other objects but simply replaced them with itself, the operation $slice pushed itself into the array and did not execute I think? I am mildly confused.
anyways, how do I fix this? how do I delete an index of the array without pushing the weird $slice object array thingi?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您注意到更新的文档,您可能会意识到您实际上没有使用
$slice
运算符。相反,您正在为数组设置一个新值。这就是为什么您看到第一个元素为$slice
某物,而第二个元素为1
。$slice
运算符与$push
一起使用,用于最终限制数组字段中的元素数量。该文档没有提到在给定索引的情况下删除数组元素。根据这个答案没有简单的方法可以做到这一点。
有
$pull
,但是它不适用于给定的索引。它基于数组中对象的查询条件来工作。因此,如果您首先计算出元素索引,然后在更新查询中使用它。您可以直接这样做。如果您想使用 JS 函数,您可以使用 splice,它可以进行就地更新。使用
$function
If you notice your updated document you might realize that you are not updating anything in the array actually using your
$slice
operator. Instead you are setting a new value for the array. And that is why you see the first element as$slice
something and the second as1
.$slice
operator is used with$push
and is used to limit the number of elements in the array field finally. The doc doesn't mention removing an array element with this, given its index.According to this answer there is no simple way to do this.
There is
$pull
, but it does not work with the given index. It works based on a query condition for the object in the array. So if you are first figuring out the element index and then using it in the update query. You can do that directly.If you want to use a JS function you can use
splice
, which does in-place updates. Use the$function