简体   繁体   English

如何将更多字段(在 mongoose 模式中)添加到一个字段(这是一个对象数组)中,并且这些对象是对另一个 mongoose 模式的引用?

[英]how to add more fields(in mongoose schema) into a field(which is an array of objects) and these objects are reference to another mongoose schema?

i have a mongoose schema of a user which has a field viewes_posts, which is an array of objects and these objects are reference to another mongoose schema (post).我有一个用户的猫鼬模式,它有一个字段viewes_posts,它是一个对象数组,这些对象是对另一个猫鼬模式(帖子)的引用。 Now i want to add two more fields inside this array of objects .现在我想在这个对象数组中再添加两个字段。

i tried it this way :我这样试过:

viewed_posts: [
    {
      hits: {type: Number, default: 0},
      actualViewsByUser: {type: Number, default: 0},
      type: mongoose.Schema.Types.ObjectId,
      ref: "Post"
    }
  ]

But when i use User.populate("viewed_posts") function it does not show me "hits" and "actualViewsByUser", instead it only shows the complete fields of post schema.但是当我使用 User.populate("viewed_posts") 函数时,它不会显示“点击数”和“actualViewsByUser”,而是只显示帖子模式的完整字段。

This is the code where i want to access those fields :这是我要访问这些字段的代码:

User.findById(req.user._id).populate("viewed_posts").exec(function(err,user){
      if(err) console.log(err);
      else{
        console.log("usre found , now we are going to update the views")
        Post.findOne({slug: req.params.slug},(err,post)=>{
          if(err) console.log(err);
          else{
            user.viewed_posts.push(post);
            user.save();
            let index= 0;
            console.log("viewed_posts.length",user.viewed_posts.length);
            for(var i=0;i<user.viewed_posts.length;i++){
              console.log("post.slug", post.slug)
              console.log("user.viewed_posts.slug", user.viewed_posts[i].slug)
              if(post.slug === user.viewed_posts[i].slug){
                console.log(user.viewed_posts[i]);
                index = i;
                break;
              }
            }
            console.log("index  : ", index);
            console.log("increasing number of hits each time this particular user visits this post");
            console.log("user.viewed_posts.hits before  : " , user.viewed_posts[index].data.hits)
            user.viewed_posts[index].hits +=1;
            console.log("user.viewed_posts.hits after  : " , user.viewed_posts[index].data.hits)
            console.log("increasing number of hits of the post no matter how many times any user visit here");
            console.log("post.views before  : " , post.views)
            post.views +=1;
            console.log("post.views after  : " , post.views)
            if(user.viewed_posts[index].actualViewsByUser != 4){
              console.log("increasing number of actualViews of this post only if this user haven't visited more than 4 times");
              console.log("post.actualViews before  : " , post.actualViews)
              post.actualViews +=1;
              console.log("post.actualViews after  : " , post.actualViews)
              post.save((err,post)=>{
                if(err) console.log(err)
                else console.log("post saved sucessfully",post);
              });
              console.log("user.viewed_posts.actualViewsByUser after  : " , user.viewed_posts[index].actualViewsByUser)
              user.viewed_posts.actualViewsByUser += 1;
              console.log("user.viewed_posts.actualViewsByUser after  : " , user.viewed_posts[index].actualViewsByUser)
            }
            
            
          }
        })
      }
    })

and this is the output :这是输出:

increasing number of hits each time this particular user visits this post
user.viewed_posts.hits before  :  undefined
user.viewed_posts.hits after  :  NaN
increasing number of hits of the post no matter how many times any user visit here
post.views before  :  0
post.views after  :  1
increasing number of actualViews of this post only if this user haven't visited more than 4 times
post.actualViews before  :  0
post.actualViews after  :  1
user.viewed_posts.actualViewsByUser after  :  undefined
user.viewed_posts.actualViewsByUser after  :  undefined

What can i do to integrate these 2 fields(hits,actualViewsByUser) into this field(viewed_posts)?我该怎么做才能将这 2 个字段(hits、actualViewsByUser)集成到这个字段(viewed_posts)中? Any help would be appreciated.任何帮助,将不胜感激。

Because you have to specify the fields you want to populate.因为您必须指定要填充的字段。

User.findById(req.user._id).populate("viewed_posts", "hits actualViewsByUser", Post) // pass in the Post model as well (you'll have to import this).

I pass in the model as I've had errors before because Mongoose didn't populate the fields for me correctly.我传递了模型,因为我之前遇到过错误,因为 Mongoose 没有正确地为我填充字段。

However, I'm not sure your approach is the best one.但是,我不确定您的方法是最好的方法。 "hits" and "actualViewsByUser" would refer to a specific post via an ID. “hits”和“actualViewsByUser”将通过 ID 引用特定的帖子。 When you use populate Mongoose will take the id, look for the relevant post, then populate with the appropriate information "hits", "actualViewsByUser", etc. The "hits" and "actualViewsByUser" would need to be stored on your Post model, not your User model.当您使用 populate Mongoose 时,将获取 id,查找相关帖子,然后使用适当的信息“hits”、“actualViewsByUser”等填充。“hits”和“actualViewsByUser”需要存储在您的 Post 模型中,不是您的 User 模型。

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

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