为什么在 MongoDB updateOne 中使用切片不会从数组中删除项目,而只是用一个奇怪的对象替换它?

发布于 2025-01-11 21:56:30 字数 661 浏览 0 评论 0原文

所以有数组课程如下 例如,在它的内部有这样的对象:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我入戏太深 2025-01-18 21:56:30

如果您注意到更新的文档,您可能会意识到您实际上没有使用 $slice 运算符。相反,您正在为数组设置一个新值。这就是为什么您看到第一个元素为 $slice 某物,而第二个元素为 1

$slice 运算符与 $push 一起使用,用于最终限制数组字段中的元素数量。该文档没有提到在给定索引的情况下删除数组元素。

根据这个答案没有简单的方法可以做到这一点。

$pull,但是它不适用于给定的索引。它基于数组中对象的查询条件来工作。因此,如果您首先计算出元素索引,然后在更新查询中使用它。您可以直接这样做。

如果您想使用 JS 函数,您可以使用 splice,它可以进行就地更新。使用 $function

User.updateOne({username: req.body.username}, [{ $set:
{ "coursesFollowing": { $function: { body: function(coursesFollowing) { coursesFollowing.splice(removeIndex, 1); return coursesFollowing; }, args: ["$coursesFollowing"], lang: "js" }} }
  }], function(err,result){
    if(err){return next(err)}
  })

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 as 1.

$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

User.updateOne({username: req.body.username}, [{ $set:
{ "coursesFollowing": { $function: { body: function(coursesFollowing) { coursesFollowing.splice(removeIndex, 1); return coursesFollowing; }, args: ["$coursesFollowing"], lang: "js" }} }
  }], function(err,result){
    if(err){return next(err)}
  })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文