Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js mongoose从数组中删除_Node.js_Mongoose - Fatal编程技术网

Node.js mongoose从数组中删除

Node.js mongoose从数组中删除,node.js,mongoose,Node.js,Mongoose,我需要从集合中的所有对象(传递的对象除外)中删除用户id,在我的示例中,它是value:'tаааa',请告诉我如何发出这样的请求 console.log(结果) 控制台日志(请求正文) 控制台日志(请求用户id) 我能做的就是找到 router.post('/', passport.authenticate('jwt', {session: false}), (req, res) => { FirstName.find({value: req.body.value}) .

我需要从集合中的所有对象(传递的对象除外)中删除用户id,在我的示例中,它是value:'tаааa',请告诉我如何发出这样的请求

console.log(结果)

控制台日志(请求正文)

控制台日志(请求用户id)

我能做的就是找到

router.post('/', passport.authenticate('jwt', {session: false}), (req, res) => {
   FirstName.find({value: req.body.value})
    .then(result => {
      if (result.length) {
        console.log(result)
        console.log(req.body)
        console.log(req.user._id)
        FirstName.find({value: {$ne: 'Слоник'}}, function (err, arr) {
            arr.map(e => {
              if (e.vote[req.body.habalkaId].length) {
                if(e.vote[req.body.habalkaId].includes(String(req.user._id))){
                  console.log(e.vote[req.body.habalkaId])
                }
              }
            })

        })

      } else {
        new FirstName({
          value: req.body.value,
          vote: {[req.body.habalkaId]: [String(req.user._id)]}
        }).save();
      }
    })

  // res.json({res: req.body})
})
FirstName.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

// Create Schema
const FirstNameSchema = new Schema({
  value: {
    type: String
  },
  vote: {
    type: Object
  },
  date: {
    type: Date,
    default: Date.now
  }
});

module.exports = FirstName = mongoose.model('firstname', FirstNameSchema);

如果我理解的很好,你想要这样的东西:

db.collection.update({
“价值”:{
$ne:“塔塔”
}
},
{
“$pull”:{
“投票.数组\u名称”:“id\u值”
}
},
{
多:真的
})
首先,查找与给定值不匹配的所有文档。然后,对于找到的每个文档,使用
id
匹配的
pull
从数组中删除对象

范例


请检查付款地,并检查我是否使用了正确的模式,它是否显示了预期的输出。

您可以发布您的收藏示例吗?添加,查看结尾问题“array_name”:[{0:“id_value”}]它不是一个对象数组,只是一个指南针显示的数组['5f63a251f17f1f38bc92bdab']这就是它的实际外观。请尝试使用这个名字。更新({“value”:{“$ne”:“tcаааa”},{“$pull”:{“vote.36e7da32-f818-4771-bb5e-1807b2954b5f”:req.user.\u id},{multi:true})但别担心,我发现了我的错误,FirstName.update({“value”:{“$ne”:“thotааа”},{“$pull”:{“vote.36e7da32-f818-4771-bb5e-1807b2954b5f”:String(req.user.\u id)},{multi:true})请粘贴它,而不是你的代码这是真的,对不起!这是
compass
输出。我已经更新了答案。
5f63a251f17f1f38bc92bdab
router.post('/', passport.authenticate('jwt', {session: false}), (req, res) => {
   FirstName.find({value: req.body.value})
    .then(result => {
      if (result.length) {
        console.log(result)
        console.log(req.body)
        console.log(req.user._id)
        FirstName.find({value: {$ne: 'Слоник'}}, function (err, arr) {
            arr.map(e => {
              if (e.vote[req.body.habalkaId].length) {
                if(e.vote[req.body.habalkaId].includes(String(req.user._id))){
                  console.log(e.vote[req.body.habalkaId])
                }
              }
            })

        })

      } else {
        new FirstName({
          value: req.body.value,
          vote: {[req.body.habalkaId]: [String(req.user._id)]}
        }).save();
      }
    })

  // res.json({res: req.body})
})
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

// Create Schema
const FirstNameSchema = new Schema({
  value: {
    type: String
  },
  vote: {
    type: Object
  },
  date: {
    type: Date,
    default: Date.now
  }
});

module.exports = FirstName = mongoose.model('firstname', FirstNameSchema);