Mongoose - 获取和删除子记录

Mongoose - Get and Delete a subrecord

我有一个这样定义的模型:

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const feedbackSchema = new Schema({
  Name: {
    type: String,
    required: true,
  },
  Email: {
    type: String,
    required: true,
  },
  Project: {
    type: String,
    required: true,
  },
  Wonder: {
    type: String,
    required: true,
  },
  Share: {
    type: String,
    required: true,
  },
  Delight: {
    type: String,
    required: true,
  },
  Suggestions: {
    type: String,
    required: true,
  },
  Rating: {
    type: String,
    required: true,
  },
  dateCreated: {
    type: Date,
    default: Date.now(),
  },
  user: {
    type: Schema.Types.ObjectId,
    ref: 'User'
  }

});

const UserSchema = new Schema({
  googleId: {
    type: String
  },
  displayName: {
    type: String
  },
  firstName: {
    type: String
  },
  lastName: {
    type: String
  },
  image: {
    type: String
  },
  createdAt: {
    type: Date,
    default: Date.now(),
  },

  feedback: [feedbackSchema],
})

module.exports = mongoose.model("User", UserSchema);

示例文档:

{
    _id: ObjectId('60b9dc728a516a4669b40dbc'),
    createdAt: ISODate('2021-06-04T07:42:01.992Z'),
    googleId: '2342987239823908423492837',
    displayName: 'User Name',
    firstName: 'User',
    lastName: 'Name',
    image: 'https://lh3.googleusercontent.com/a-/89wf323wefiuhh3f9hwerfiu23f29h34f',
    feedback: [
        {
            dateCreated: ISODate('2021-06-04T07:42:01.988Z'),
            _id: ObjectId('60b9dc858a516a4669b40dbd'),
            Name: 'Joe Bloggs',
            Email: 'joe@bloggs.com',
            Project: 'Some Project',
            Suggestions: 'Here are some suggestions',
            Rating: '10'
        },
        {
            dateCreated: ISODate('2021-06-04T08:06:44.625Z'),
            _id: ObjectId('60b9df29641ab05db7aa2264'),
            Name: 'Mr Bungle',
            Email: 'mr@bungle',
            Project: 'The Bungle Project',
            Suggestions: 'Wharghable',
            Rating: '8'
        },
        {
            dateCreated: ISODate('2021-06-04T08:08:30.958Z'),
            _id: ObjectId('60b9df917e85eb6066049eed'),
            Name: 'Mike Patton',
            Email: 'mike@patton.com',
            Project: 'No More Faith',
            Suggestions: 'Find the faith',
            Rating: '10'
        },
    ],
    __v: 0
}

我定义了两条路线,当用户单击 UI 上反馈项上的按钮时调用第一个路线,将用户带到“您确定要删除此记录吗”类型的页面,显示 selected 反馈记录中的一些信息。

第二条路线,当用户单击 'confirm' 时,子记录将从文档中删除。

我遇到的问题是我似乎无法从用户那里获取反馈以便通过 id select 文档,这是我到目前为止的确认路线:

router.get('/delete', ensureAuth, async (req, res) => {
    try {
        var url = require('url');
        var url_parts = url.parse(req.url, true);
        var feedbackId = url_parts.query.id;

        const allFeedback = await User.feedback;
        const feedbackToDelete = await allFeedback.find({ _id: feedbackId });

        console.log(feedbackToDelete);

        res.render('delete', {
            imgSrc: user.image,
            displayName: user.firstName,
            feedbackToDelete
        });
    } catch (error) {
        console.log(error);
    }
})

非常感谢帮助

更新

您应该能够做到这一点:

const feedbackToDelete = await User.feedback.find({ _id: feedbackId });

或者如果 feedbackId 只是一个字符串,看起来是这样,您可能必须执行以下操作:

// Create an actual _id object
// That is why in your sample doc you see ObjectId('foobarbaz')
const feedbackId = new mongoose.Types.ObjectId(url_parts.query.id);
const feedbackToDelete = await User.feedback.find({ _id: feedbackId });

原创

不应该这样:

const allFeedback = await User.feedback;(一个字段)

是这样的:

const allFeedback = await User.feedback(); (一个method/function)

?