简体   繁体   English

如何在 MongoDB 中通用更新字段?

[英]How to update a field universally in MongoDB?

I have 1 database: database .我有 1 个数据库: database There are 2 collections in database: users , posts .数据库中有 2 个 collections: usersposts Mongoose schemas: User , Post . Mongoose 模式: UserPost

Here is a user :这是一个user

{
  "_id": "5f0e99abef55507333f0c34ef45",
  "name": "David",
  "email": "david@email.com"
}

I can post something with the user and save it to the posts collection.我可以与user post一些内容并将其保存到posts集合中。

let user = await User.findById(req.user.id);
let newPost = new Post({
  post: req.body.post,
  by: user, // this is the important part
});
await newPost.save();

And here is the new post with a by field:这是带有by字段的新post

"_id": "3287327392392293243434",
"post": "this is david's post",
"by": {
  "_id": "5f0e99abef55507333f0c34ef45",
  "name": "David",
  "email": "david@email.com"
  }

And then I update my profile to this:然后我将我的个人资料update为:

{
  "_id": "5f0e99abef07333f0c34ef45",
  "name": "Dave", // change from David to Dave
  "email": "david@email.com"
}

After this I send a get request to get all posts:在此之后,我发送一个get请求以获取所有帖子:

[{
"_id": "3287327392392293243434",
"post": "this is david's post",
"by": {
  "_id": "5f0e99abef07333f0c34ef45",
  "name": "David",
  "email": "david@email.com"
  },
}]

The problem is that the by field has the same object, nothing was updated here after I updated my name.问题是by字段具有相同的 object,在我更新我的名字后这里没有更新。 It's still David and not Dave.仍然是大卫而不是戴夫。 I know that when I updated my profile I only updated the user, not the post.我知道当我更新我的个人资料时,我只更新了用户,而不是帖子。 I could write the code like to update the post too, but if I have many posts, many comments, and I change my name, image, biography or other fields regularly, then way too many things happen when updating my profile (and maybe thousands of posts).我也可以编写代码来更新帖子,但是如果我有很多帖子、很多评论,并且我定期更改我的姓名、图像、传记或其他字段,那么在更新我的个人资料时会发生太多事情(可能有数千个帖子)。

What I want is that when I update my user profile, is there a way to be updated across this MongoDB database?我想要的是,当我更新我的user配置文件时,有没有办法在这个 MongoDB 数据库中进行更新? Like I want that by field in the post to show my updated name.就像我希望postby字段显示我的更新名称一样。

Is it something to do with the user schema?它与用户模式有关吗?

The problem you are facing is because you are saving duplicate data of user in your database, once in user collection and once in post collection.您面临的问题是因为您将用户的重复数据保存在数据库中,一次在用户集合中,一次在后期集合中。 You shouldn't have duplicate data in your database.您的数据库中不应有重复的数据。

Instead of saving complete user data in by field with each post document, only save user's id and link both user document and post document using SchemaType.prototype.ref() .而不是在每个帖子文档中by字段保存完整的用户数据,只保存用户的 id 并使用SchemaType.prototype.ref()链接用户文档和帖子文档。 This way, you won't have duplicate data in your database and the problem you are facing right now will be solved.这样,您的数据库中将不会有重复的数据,您现在面临的问题将得到解决。 Post schema should like:后架构应该像:

let postSchema = new mongoose.Schema({
  post: String,
  by: { type: Schema.Types.ObjectId, ref: 'User' } // link between post and a user
});

and User schema should look something like:和用户模式应该是这样的:

let userSchema = new mongoose.Schema({
  name: String,
  email: String 
});

Keep in mind that value of ref in post schema should be same as the string that you pass as first argument to mongoose.model() function.请记住,后架构中的ref值应与您作为第一个参数传递给mongoose.model() function 的字符串相同。 For example, if in user schema file, you call mongoose.model('User', userSchema) then the value of ref property in post schema should be 'User'例如,如果在用户模式文件中调用mongoose.model('User', userSchema)那么后模式中ref属性的值应该是'User'

Take a look at Mongoose - Populate() .看看Mongoose - Populate() This function will allow you to get the user data that is linked with any particular post document.此 function 将允许您获取与任何特定过帐文档链接的用户数据。

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

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