【问题标题】:How can I check if the user already follows other users in NodeJs - Mongoose如何检查用户是否已经关注 NodeJs 中的其他用户 - Mongoose
【发布时间】:2017-06-30 19:59:40
【问题描述】:

我有一个 followSchema:

var followSchema = mongoose.Schema({ 
    user : { type: Number, ref: 'User'},
    following : { type: Number, ref: 'User'}
});

我有两个用户。 在第一个中,我需要列出他关注的所有用户。 第二个我需要检查他是否已经关注了我从第一个收集的一些用户。

如何在 Mongoose 中以有效的方式做到这一点。 从第一个用户那里获取关注者列表并不难,但要检查第二个用户是否关注其中一个就有点困难了,我不知道我该如何管理。

【问题讨论】:

    标签: javascript node.js mongodb mongoose mongodb-query


    【解决方案1】:

    这很简单,你需要做的就是查询这两个条件。假设“Follow”为模型:

    Follow.find({
       "user": 1,
       "following": 2
    })
    

    不过,根据您的“规模”,以下架构可能更有用:

    var userSchema = new Schema({
        "_id": Number,
        "name": String,
        "following": [{ "type": Number, "ref": "User" }],
        "followedBy": [{ "type": Number, "ref": "User" }]
    })
    

    查询几乎相同:

    User.find({
        "_id": 1,
        "following": 2
    })
    

    当然,它总是为您提供其他选择,只要可以遵守“数组”最佳实践的一般限制条件。

    【讨论】:

    • 如果我使用嵌入式文档,那么它的扩展性不是很好,是吗?如果我有一个拥有数百万粉丝的用户,那会有点困难,不是吗?
    【解决方案2】:

    您可以采取的一种方法是修改您的 followSchema 以将 following 字段作为数组并使用 population 的概念:

    var mongoose = require('mongoose')
      , Schema = mongoose.Schema
    
    var userSchema = Schema({
        _id: Number,
        name: String,
        age: Number,
        followers: [{ type: Schema.Types.ObjectId, ref: 'Follow' }]
    });
    
    var followSchema = Schema({
        _user: { type: Number, ref: 'User' },
        following: [{ type: Number, ref: 'User' }]
    });
    
    var Follow  = mongoose.model('Follow', followSchema);
    var User = mongoose.model('User', userSchema);
    

    然后您可以使用一些示例用户进行查询。作为示例指南(未经测试):

    var user1_id = 1,
        user2_id = 2,    
        user3_id = 3,   
        user1_following = [], 
        user2_following = [];
    
    var user1 = new User({ _id: user1_id, name: 'User1', age: 31 });
    var user2 = new User({ _id: user2_id, name: 'User2', age: 32 });
    var user3 = new User({ _id: user3_id, name: 'User3', age: 32 });
    
    user3.save(function (err) {
         if (err) return handleError(err);       
    })
    
    user1.save(function (err) {
         if (err) return handleError(err);  
         var follower3 = new Follow({ _user: user3_id });           
         follower3.save(function (err) {
            if (err) return handleError(err);
            // thats it!
         });
    })
    
    user2.save(function (err) {
         if (err) return handleError(err);  
         var follower3 = new Follow({ _user: user3_id });         
         follower3.save(function (err) {
            if (err) return handleError(err);
            // thats it!
         });
    })
    
    Follow
        .find({ _user: user1_id })
        .exec(function (err, following) {
             if (err) return handleError(err);
             user1_following = following;
       })    
    
    Follow
        .find({ _user: user2_id })
        .exec(function (err, following) {
             if (err) return handleError(err);
             user2_following = following;
       }) 
    

    然后您可以使用 Underscore 的 intersection(),它会为您提供两个数组中存在的值的列表。

    var commonFollowings = _.intersection(user1_following, user2_following);
    // In the example above, commonFollowings => [3]
    

    【讨论】:

    • 意思是在followSchema中使用嵌入文档?现在我正在使用多个集合
    • 不,您将只使用对那些多个集合中的文档的引用,这是 Mongoose 种群的概念。我已经更新了我的答案,包括一个粗略的指导方针作为你的模型的例子。
    • 好的,谢谢,但是如果你有很多粉丝,你会不会超过 16mb 的限制?
    • 我认为你的问题最好用这个answer来解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    相关资源
    最近更新 更多