更新 MongoDB 节点内的对象数组。

huangapple go评论62阅读模式
英文:

Updating array of objects inside an object mongodb node

问题

以下是您要翻译的MongoDB模型和相关问题部分:

// MongoDB模型定义如下:
var mongoose = require("mongoose");

const postModel = new mongoose.Schema({
    postId: {
        type: String,
        unique: true,
        required: true
    },
    authorId: {
        type: String,
        required: true
    },
    post: {
        authorHandle: {
            type: String,
            required: true
        },
        heading: {
            type: String,
            required: true
        },
        message: {
            type: String,
            required: true
        },
        creationDate: {
            type: Date,
            required: true
        },
        image: { type: Array }, 
        video: { type: Array },
        comments: {
            type: Array
        }
    }
});

module.exports = mongoose.model("postModel", postModel);

现在我有上述模型的文档示例值,假设如下:

postId: "aaa",
authorId: "bbb",
post: {
  authorHandle: "someone#123",
  heading: "hello",
  message: "post 1",
  creationDate: "一些创建日期字符串(请忽略,与我的问题无关)",
  image: [],
  video: [],
  comments: [
   { commentId: "1", message: "Something", createdAt: sometime },
   { commentId: "2", message: "Something else", createdAt: sometime2 },
   { commentId: "3", message: "Something other", createdAt: sometime3 },
  ]
}

现在假设用户想要更新帖子 "postId" 为 "aaa" 的评论中的 "commentId" 为 "2" 的部分。我的问题是,使用 "findOneAndUpdate()" 方法来解决这个问题的最佳方法是什么?

const PostModel = require("./models/PostModel"); // 仅导入上面定义的模型
// 以下代码在Node + Express中的请求处理程序中执行
PostModel.findOneAndUpdate(
  // 在这里应该怎么做
)

我尝试过将整个对象提取出来,然后用新消息的新对象替换它。但这似乎不是一种非常高效的方法。非常感谢您的任何帮助!


<details>
<summary>英文:</summary>

I have a mongoDb model defined as follows:-

var mongoose = require("mongoose");

const postModel = new mongoose.Schema({
postId: {
type: String,
unique: true,
required: true
},
authorId: {
type: String,
required: true
},
post: {
authorHandle: {
type: String,
required: true
},
heading: {
type: String,
required: true
},
message: {
type: String,
required: true
},
creationDate: {
type: Date,
required: true
},
image: { type: Array },
video: { type: Array },
comments: {
type: Array
}
}
});

module.exports = mongoose.model("postModel", postModel);

Now I have a sample value of a document of the above model, suppose:-

postId: "aaa",
authorId: "bbb",
post: {
authorHandle: "someone#123",
heading: "hello",
message: "post 1",
creationDate: "some creation date string(please ignore this is irrelevant to my question)",
image: [],
video: [],
comments: [
{ commentId: "1", message: "Something", createdAt: sometime },
{ commentId: "2", message: "Something else", createdAt: sometime2 },
{ commentId: "3", message: "Something other", createdAt: sometime3 },
]
}

Now say the user wants to update the comment with commentId ``2`` of this post with ``postId`` &quot;aaa&quot;. My question is that what is the best way to use the ``findOneAndUpdate()`` method to solve this problem?
```js
const PostModel = require(&quot;./models/PostModel&quot;); //just importing the model that is defined above
//the below is happening inside a request handler in Node + Express
PostModel.findOneAndUpdate(
  //what to do here
)

What I have tried is pulling out that whole object and replacing it with a new object with the new message. But that doesnt seem like a very efficient way. Any and all help is greatly appreciated!

答案1

得分: 1

const updatedPost = await PostModel.findOneAndUpdate(
  { postId: 'aaa', 'post.comments.commentId': 2 },
  { 'post.comments.$.message': '更新的消息'},
  { new: true }
)
英文:

You should write:

const updatedPost = await PostModel.findOneAndUpdate(
  { postId: &#39;aaa&#39;, &#39;post.comments.commentId&#39;: 2 },
  { &#39;post.comments.$.message&#39;: &#39;Updated message&#39;},
  { new: true }
)

huangapple
  • 本文由 发表于 2023年1月6日 15:05:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定