0

I need to remove the user's id from all objects in the collection except the one that was passed, in my example it is value: 'Тата', tell me how to make such a request?

enter image description here

console.log(result)

[
 {
    _id: 5fa702b2f18e5723b4c00d9f,
    value: 'Тата',
    vote: { '36e7da32-f818-4771-bb5e-1807b2954b5f': [Array] },
    date: 2020-11-07T20:25:22.611Z,
    __v: 0
  }
]

console.log(req.body)

{ value: 'Тата', habalkaId: '36e7da32-f818-4771-bb5e-1807b2954b5f' }

console.log(req.user._id)

5f63a251f17f1f38bc92bdab

that's all I could do, just find

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);
2
  • Can you post your collection example, please? Commented Nov 9, 2020 at 12:39
  • added, look in end question Commented Nov 9, 2020 at 12:43

1 Answer 1

1

If I've understand well, you want something like this:

db.collection.update({
  "value": {
    "$ne": "tata"
  }
},
{
  "$pull": {
    "vote.array_name": "id_value"
  }
},
{
  multi: true
})

First of all, find all document that not match the value with the given one. Then, for each document found, delete the object from the array, using $pull where the id given matches.

Example here

Please check the payground and check if I've used the correct schema and it shows the expected output.

Sign up to request clarification or add additional context in comments.

4 Comments

"array_name": [ { 0: "id_value" } ] it is not an array of objects, but just an array just the compass shows so [ '5f63a251f17f1f38bc92bdab' ] this is how it actually looks
i try this FirstName.update({ "value": { "$ne": "Тата" } }, { "$pull": { "vote.36e7da32-f818-4771-bb5e-1807b2954b5f": req.user._id } }, { multi: true }) but don't work
ok i found my mistake, FirstName.update({ "value": { "$ne": "Тата" } }, { "$pull": { "vote.36e7da32-f818-4771-bb5e-1807b2954b5f": String(req.user._id) } }, { multi: true }) paste it please instead of your code
That's true, sorry! That's the compass output. I've updated the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.