代码之家  ›  专栏  ›  技术社区  ›  G. Ball.

mongoose模式的最有效结构

  •  0
  • G. Ball.  · 技术社区  · 6 年前

    我很好奇构造猫鼬模式的最佳方法是什么。我正在制作一个用户模式设置如下的电影网站:

    let UserSchema = new mongoose.Schema({
        username: {
            type: String,
            index: true
        },
        password: {
            type: String
        },
        email: {
            type: String
        },
        name: {
            type: String
        },
        watchlist: [mongoose.Schema.Types.Mixed],
        seen: {
            like : {
                type: [mongoose.Schema.Types.Mixed]
            },
            dislike: {
                type: [mongoose.Schema.Types.Mixed]
            }
        },
    });
    

    当用户单击向上或向下的拇指图标时,对象

    {movieID: movieID, title: movieTitle, poster: moviePoster}

    将被发送到seen属性中的like或dislike数组。我将在我的EJS模板中使用这个信息来确定是否已经看过一部电影,如果看过并且喜欢,那么用灰色表示不喜欢按钮,如果看过并且不喜欢,用灰色表示喜欢按钮。我还想能够删除项目,从看到如果使用点击一个'清除'按钮。为了做到这一点,我似乎要循环两个数组,以确定电影是否包含在一个数组中,然后删除它。看来一定有更好的办法。我在考虑用movieID作为键来构造“seed”对象,如下所示:

    seen: {
      '123': {
        movieID: '123',
        title: 'movieA',
        poster: 'movieA-poster.jpg',
        like: true
      }, 
      '456' : {
        movieID: '456',
        title: 'movieB',
        poster: 'movieB-poster.jpg',
        like: false
      }
    }
    

    const movieObj = {
      id: req.body.movieID,
      title: req.body.movieTitle,
      poster_path: req.body.poster_path,
    };
    
    User.findOne({_id: req.user._id}, function(err, user) {
      if (err) {
        console.log(err);
      } else {
    
        user.seen[req.body.movieID] = movieObj;
        user.save(function(){});
      }
    }
    

    我还是得到了一个空的东西。我将schema seen对象更改为:

    seen: { type: mongoose.Schema.Types.Mixed, default: {} }
    

    并且设置了{minimize:false},因为我读到mongoose在默认情况下不允许空对象。感谢任何关于我做错了什么的指导,或者您是否有更好的方法来有效地构建模式,以便可以轻松地从db中添加或删除所看过的电影。

    2 回复  |  直到 6 年前
        1
  •  2
  •   Sagar Chaudhary    6 年前


    只要读一读,你就会对它有个好主意。 下面是一个很好的示例链接:

    https://hashnode.com/post/how-do-i-successfully-populate-a-mongoose-schema-cj339r6kt004g5mk83ycujilq

        2
  •  1
  •   IftekharDani    6 年前

    模式

    1 用户架构

    let UserSchema = new mongoose.Schema({
        username: {
            type: String,
            index: true
        },
        password: {
            type: String
        },
        email: {
            type: String
        },
        name: {
            type: String
        },
        watchlist: [mongoose.Schema.Types.Mixed],
        like : [mongoose.Schema.Types.ObjectId], //remove
        deslike : [mongoose.Schema.Types.ObjectId], //remove
        seen : [{
        movieId : { type:  : mongoose.Schema.Types.ObjectId,  ref: 'Movie' },
        isStatus : {type : string} //Enum [like,dislike,'seen']
    }]
    })
    

    let movieSchema = new mongoose.Schema({
        tile: {
        type: String
      },
      description: { type: String }
    })
    

    3.两个表中的数据存储

    / 用户

     {
            "_id" : ObjectId("5b5acaf0589ff6bfb5dd091f"),
    "seen" : [ 
            {
                "movieId" : ObjectId("5b9e2544953b5f69683059d4"),
                "isStatud" : "like"
            }, 
            {
                "movieId" : ObjectId("5b9e2544953b5f69683059d6"),
                "isStatud" : "dislike"
            }, 
            {
                "movieId" : ObjectId("5b9e256d953b5f69683059ee"),
                "isStatud" : "seen"
            }
        ]
            "like" : [ 
                ObjectId("5b9e2544953b5f69683059d4"), 
                ObjectId("5b9e256d953b5f69683059ee")
            ],
            "deslike" : [ 
                ObjectId("5b9e2544953b5f69683059d6")
            ]
        }
    

    / 电影

    {
        "_id" : ObjectId("5b9e2544953b5f69683059d4"),
        "title" : "movie1",
        "description" : "desc1"
    }
    
    {
        "_id" : ObjectId("5b9e2544953b5f69683059d6"),
        "title" : "movie2",
        "description" : "desc2"
    }
    
    {
        "_id" : ObjectId("5b9e256d953b5f69683059ee"),
        "title" : "movie3",
        "description" : "desc3"
    }
    

    查询以按电影获取用户

    /.

    db.getCollection('user').aggregate([{
        $match : { _id : ObjectId("5b5acaf0589ff6bfb5dd091f") }
        },
        {$lookup : {
            from : 'movie',
            localField : 'like',
            foreignField : '_id',
            as : "likes"
            }},
    
            {$lookup : {
            from : 'movie',
            localField : 'deslike',
            foreignField : '_id',
            as : "deslikes"
            }}
    
        ])
    

    / 使用组查询

    db.getCollection('user').aggregate([{
        $match : { _id : ObjectId("5b5acaf0589ff6bfb5dd091f") }
        },
        {$unwind : '$seen'},
        {$lookup : {
            from : 'movie',
            localField : 'seen.movieId',
            foreignField : '_id',
            as : "seen.movieData"
            }},
        {$unwind : '$seen.movieData'},
        { $group: { "_id": "$_id", 
                  "name" : { "$first": "$name" }, //use same all other field of user
                  "seen" : {"$push" : "$seen"}
                   } ,
                    }
        ])
    

    请检查并告诉我有什么帮助。