猫鼬更新子文档
Mongoose update Sub-Document
如何在猫鼬中更新子文档:
这是我的JSON数据
[
{
_id: "60215bb12390573490fb30c4",
publishedAt: "2021-02-08T15:41:28.562Z",
comments: [
{
messageAt: "2021-02-08T15:47:04.197Z",
_id: "60215d92f16e9f208c8663a1",
message: "beautiful picture",
userId: "600c26312c1e41372015e834",
},
{
messageAt: "2021-02-09T13:55:49.414Z",
_id: "60229495e285843da095f84e",
message: "wonderful view",
userId: "600c26312c1e41372015e834",
},
]
}
]
这是 post 架构。
const postSchema = new mongoose.Schema({
userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required:
true },
photos: [{
type: String,
required: true
}],
description: {
type: String,
required: true
},
publishedAt: {
type: Date,
default: new Date()
},
likes: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }],
comments: [
{
message: { type: String, required: true },
messageAt: { type: Date, default: new Date() },
userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User',
required: true },
}
]
});
这是 NodeJs 部分
router.patch('/comment/:postId/:commentId', auth, async (req, res) => {
try {
const { postId, commentId } = req.params;
const post = await Post.findById(postId);
if (!post) return res.status(400).send('Invalid Post');
post.comments.update(
{ _id: commentId },
{
$set: { message: req.body.message }
}
)
res.send('Comment updated successfully');
} catch (error) {
res.status(400).send(error.message);
}
});
**
我在 Postman 中得到了这个回复:post.comments.update 不是它接受的函数 post.update 但它不是我想要的,因为 属性 消息在每个函数的子文档中post 对象,任何帮助请
**
你应该运行方法Model.update() on the Model "Post" not the instace "post" of it. A similar question is answered 。
如何在猫鼬中更新子文档:
这是我的JSON数据
[
{
_id: "60215bb12390573490fb30c4",
publishedAt: "2021-02-08T15:41:28.562Z",
comments: [
{
messageAt: "2021-02-08T15:47:04.197Z",
_id: "60215d92f16e9f208c8663a1",
message: "beautiful picture",
userId: "600c26312c1e41372015e834",
},
{
messageAt: "2021-02-09T13:55:49.414Z",
_id: "60229495e285843da095f84e",
message: "wonderful view",
userId: "600c26312c1e41372015e834",
},
]
}
]
这是 post 架构。
const postSchema = new mongoose.Schema({
userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required:
true },
photos: [{
type: String,
required: true
}],
description: {
type: String,
required: true
},
publishedAt: {
type: Date,
default: new Date()
},
likes: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }],
comments: [
{
message: { type: String, required: true },
messageAt: { type: Date, default: new Date() },
userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User',
required: true },
}
]
});
这是 NodeJs 部分
router.patch('/comment/:postId/:commentId', auth, async (req, res) => {
try {
const { postId, commentId } = req.params;
const post = await Post.findById(postId);
if (!post) return res.status(400).send('Invalid Post');
post.comments.update(
{ _id: commentId },
{
$set: { message: req.body.message }
}
)
res.send('Comment updated successfully');
} catch (error) {
res.status(400).send(error.message);
}
});
**
我在 Postman 中得到了这个回复:post.comments.update 不是它接受的函数 post.update 但它不是我想要的,因为 属性 消息在每个函数的子文档中post 对象,任何帮助请
**
你应该运行方法Model.update() on the Model "Post" not the instace "post" of it. A similar question is answered