使用Mongoose更新MongoDB集合中的多个文档实例。 save()不是功能

发布于 2025-02-04 21:02:52 字数 1386 浏览 1 评论 0 原文

在这里蒙古Newbe。当标签删除时,我在文档帖子中获取了以下功能,以更新文档帖子中的引用(删除它们)。当我调用我的GraphQl API时,这是我得到的:

message": "posts.save is not a function"

GQL Resolver中的功能:

          async deleteTag(root, { id }, context) {
                const posts = await Post.find();
                const tag = await Tag.findById(id);
                if(!tag){
                    const error = new Error('Tag not found!');
                    error.code = 404;
                    throw error;
                }
                posts?.forEach(async (post) => {
                    await post.tags.pull(id);
                })
                await posts.save()
                await Tag.findByIdAndRemove(id);
                return true;
            }

这是帖子模型:

const PostSchema = new Schema({
    body: {
        type: String,
        required: true
    },
    tags: {
        type: [Schema.Types.ObjectId],
        ref: 'Tag',
        required: false
    },
}); 

这是标签模型:

const TagSchema = new Schema(
    {
        name: {
            type: String,
            required: true
        },
    },
    { timestamps: true }
);

看起来我无法称呼方法 save()在由 erocy.find()返回的对象上

,我在其他函数中使用了相同的模式,区别在于我使用 .findbyid()

是否有任何解决方案?咨询和最佳实践Advide非常欢迎。

Mongoose newbe here. I got the following function to update the references (deleting them) in the document Post when a Tag is deleted. When I call my GraphQl API this is what I got:

message": "posts.save is not a function"

The function in my gql resolver:

          async deleteTag(root, { id }, context) {
                const posts = await Post.find();
                const tag = await Tag.findById(id);
                if(!tag){
                    const error = new Error('Tag not found!');
                    error.code = 404;
                    throw error;
                }
                posts?.forEach(async (post) => {
                    await post.tags.pull(id);
                })
                await posts.save()
                await Tag.findByIdAndRemove(id);
                return true;
            }

This is the Post model:

const PostSchema = new Schema({
    body: {
        type: String,
        required: true
    },
    tags: {
        type: [Schema.Types.ObjectId],
        ref: 'Tag',
        required: false
    },
}); 

and this is the Tag model:

const TagSchema = new Schema(
    {
        name: {
            type: String,
            required: true
        },
    },
    { timestamps: true }
);

Looks like I can't call the method save() on the array of objects returned by Exercise.find()

I used the same pattern in other functions, the difference is that there I used .findById()

Any solution? Advice and best practice advide are super welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浸婚纱 2025-02-11 21:02:53

您必须单独保存帖子:

posts?.forEach(async (post) => {
  await post.tags.pull(id);
  await post.save();
})

或使用 操作员

fwiw,您可能应该通过仅选择列出的特定标签的文档来限制匹配 post 文档的数量:

await Post.find({ 'tags._id' : id });

You have to save the posts individually:

posts?.forEach(async (post) => {
  await post.tags.pull(id);
  await post.save();
})

Or use Model.updateMany() combined with the $pull operator.

FWIW, you should probably limit the number of matching Post documents by selecting only documents that have the specific tag listed:

await Post.find({ 'tags._id' : id });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文