简体   繁体   English

删除mongodb中的数组元素

[英]delete array element inside mongodb

So my mongodb looks like this: 所以我的mongodb看起来像这样:

[
  {
    "_id": "582bc918e3ff1bf021ae8b66",
    "boardName": "Test Board",
    "created_at": 1479264483957,
    "__v": 0,
    "person": [
      {
        "name": "Steve",
        "wins": 1001,
        "losses": 20,
        "_id": "582bc918e3ff1bf021ae8b69"
      },
      {
        "name": "Bobby",
        "wins": 500,
        "losses": 54,
        "_id": "582bc918e3ff1bf021ae8b68"
      },
      {
        "name": "Sarah",
        "wins": 396,
        "losses": 675,
        "_id": "582bc918e3ff1bf021ae8b67"
      }
    ]
  }
]

I want to be able to make a delete request on: 我希望能够提出以下删除请求:

/api/scoreBoard/person/:person_id and it should then remove the entire person element in my database. /api/scoreBoard/person/:person_id ,然后它将删除我数据库中的整个person元素。

So for example, if I make a delete on: 因此,例如,如果我删除以下内容:

http://localhost:3000/api/scoreBoard/person/582bc918e3ff1bf021ae8b68 it should then delete "bobby". http://localhost:3000/api/scoreBoard/person/582bc918e3ff1bf021ae8b68 ,然后应删除“ bobby”。 And the database would look like this afterwards: 然后数据库将如下所示:

[
  {
    "_id": "582bc918e3ff1bf021ae8b66",
    "boardName": "Test Board",
    "created_at": 1479264483957,
    "__v": 0,
    "person": [
      {
        "name": "Steve",
        "wins": 1001,
        "losses": 20,
        "_id": "582bc918e3ff1bf021ae8b69"
      },
      {
        "name": "Sarah",
        "wins": 396,
        "losses": 675,
        "_id": "582bc918e3ff1bf021ae8b67"
      }
    ]
  }
]

But how do I delete a person inside "person"? 但是,如何删除“人”内的人? I can't get it to work. 我无法正常工作。 This is my code so far, and it returns "Deleted person successfuly!" 到目前为止,这是我的代码,它返回“成功删除人!” but nothing happens in the database. 但数据库中没有任何反应。 It doesn't delete anything. 它不会删除任何内容。

app.delete('/api/scoreBoard/person/:person_id', function(req, res) {
  mongoose.model('scoreBoard').findByIdAndRemove(req.params.person_id, function(err) {
    if(!err) {
      res.json('Deleted person successfuly!');
    } else {
      res.write('Delete failed..');
    }
  });
});

Since you seem to be using Mongoose, I'd recommend looking at using refs and a seperate Person model for storing this information, this would look like: 由于您似乎正在使用Mongoose,因此建议您使用refs和单独的Person模型来存储此信息,如下所示:

Scoreboard: 计分板:

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

var scoreboardSchema = new Schema({
  boardName: String,
  created_at: Date,
  people: [{type: Schema.Types.ObjectID, ref:"Person"}]
});

Person: 人:

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

var personSchema = new Schema({
  name: String,
  wins: Number,
  losses: Number
});

When you want to add a user to the scoreboard you then need only push their id to the array people. 如果要将用户添加到记分板,则只需将其ID推送到数组人员。

Then when you wanted to remove a person you could just use: 然后,当您想删除一个人时,您可以使用:

db.Scoreboard.people.pull(req.params.person_id);

If you were to go down this route you would then need to populate people when you grabbed the scoreboard so that would look like: 如果您要沿着这条路线走,那么当您抓住计分板时,您将需要填充人员,因此看起来像:

Scoreboard.find({}).populate('people').exec(function(err,docs){})

For example. 例如。 This would then allow you much more freedom over the Person object, such as adding more attributes and being able to edit them much more easily. 然后,这将使您对Person对象具有更大的自由度,例如添加更多属性并能够更轻松地对其进行编辑。

Hopefully this helps somewhat. 希望这会有所帮助。

Your need to use update since You don't want to remove entire document. 由于您不想删除整个文档,因此需要使用更新

Something like this should work. 这样的事情应该起作用。

mongoose.model('scoreBoard')
.update({ 
     _id: <boardID>
},{
    $pull: { person: { _id: <personID}}
}, function(err, result){

});
app.delete('/api/scoreBoard/person/:person_id', function(req, res) {
  mongoose.model('scoreBoard').findAndUpdate({'person._id':req.params.person_id},{$pull : {person : {_id : req.params.person_id}}}, function(err) {
    if(!err) {
      res.json('Deleted person successfuly!');
    } else {
      res.write('Delete failed..');
    }
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM