【问题标题】:Likes count in Node.js MongooseNode.js Mongoose 中的点赞数
【发布时间】:2019-06-03 01:52:44
【问题描述】:

我正在构建具有喜欢、cmets、分享功能的社交应用程序。所以我有 Post 模型、User 模型和 Like 模型。所以现在我想得到他们喜欢的所有帖子,评论计数。

我可以通过此代码获得每个帖子的所有赞

在帖子模型中:

PostSchema.virtual('likes', {
    ref: 'Like',
    localField: '_id',
    foreignField: 'postId',
});

在后期控制器中:

Post.find(query).sort([['updatedAt', 'descending']])
        .populate('postedBy likes', '-__v -hash')

根据猫鼬文档here 如果我们在虚拟中使用"count": true,我们可以得到计数,但我仍然得到喜欢的数组而不是喜欢计数。

post.model.js 的完整代码

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const PostSchema = new Schema({
    title: {type: String, required: [true, 'Please enter title']},
    description: {type: String},
    category: {type: String},
    enableNotification: {type: Boolean},
    privacy: {type: String},
    tags: {type: Array},
    coverImage: {type: String},
    media: {type: Array},
    address: {type: String},
    latitude: {type: Number},
    longitude: {type: Number},
    postedBy: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true
    },

}, {
    timestamps: true,
    toObject: {
        virtuals: true
    },
    toJSON: {
        virtuals: true
    }
});

PostSchema.virtual('likeCount', {
    ref: 'Like',
    localField: '_id',
    foreignField: 'postId',
    count: true
});

PostSchema.virtual('likes', {
    ref: 'Like',
    localField: '_id',
    foreignField: 'postId',
});

PostSchema.virtual('comments', {
    ref: 'Comment',
    localField: '_id',
    foreignField: 'postId',
});

PostSchema.virtual('shares', {
    ref: 'Share',
    localField: '_id',
    foreignField: 'postId',
});

PostSchema.index({title: 'text', description: 'text', address: 'text'});

//Export the model
module.exports = mongoose.model('Post', PostSchema);

这是上面代码的结果

{
            "tags": [
                "Ee",
                "Ww",
                "Qq"
            ],
            "media": [
                ""
            ],
            "_id": "5c31919c598d1440c5af15c2",
            "postedBy": {
                "_id": "5c2ce506ca46ef2765125eca",
                "name": "Pawneshwer Gupta",
                "email": "learnpainless@gmail.com",
                "socialId": "117121382942158088901",
                "avatar": "https://lh4.googleusercontent.com/-Y5Y87PhFToI/AAAAAAAAAAI/AAAAAAAAAAA/AKxrwcblu_UMku61XayTF4z0sMJiD5C8Eg/s96-c/photo.jpg",
                "createdAt": "2019-01-02T16:21:26.035Z",
                "updatedAt": "2019-01-06T07:44:10.399Z",
                "address": "Google Shuttle Stop @ Building 40/43",
                "dob": "2019-01-06T13:14:06.068Z",
                "gender": "Male",
                "latitude": 37.422194600000005,
                "longitude": -122.08336399999999,
                "occupation": "Engineer",
                "zipCode": "140001"
            },
            "title": "Wwww",
            "description": "Ffff",
            "category": "Sports",
            "coverImage": "https://firebasestorage.googleapis.com/v0/b/onwo-1519446289456.appspot.com/o/5c2ce506ca46ef2765125eca%2Fmedia%2FMEDIA_71c79fd0-0b50-11e9-f423-cbfba233f20d.jpg?alt=media&token=0aed8a83-2899-496c-bd24-10e375285146",
            "enableNotification": true,
            "privacy": "Public",
            "createdAt": "2019-01-06T05:26:52.049Z",
            "updatedAt": "2019-01-06T05:26:52.049Z",
            "likes": [
                {
                    "_id": "5c31b9384821eb66da070b81",
                    "userId": "5c2ce506ca46ef2765125eca",
                    "postId": "5c31919c598d1440c5af15c2",
                    "createdAt": "2019-01-06T08:15:52.747Z",
                    "updatedAt": "2019-01-06T08:15:52.747Z"
                },
                {
                    "_id": "5c31b99a4821eb66da070b82",
                    "userId": "5c2ce506ca46ef2765125eca",
                    "postId": "5c31919c598d1440c5af15c2",
                    "createdAt": "2019-01-06T08:17:30.805Z",
                    "updatedAt": "2019-01-06T08:17:30.805Z"
                },
                {
                    "_id": "5c31baae4821eb66da070b83",
                    "userId": "5c2ce506ca46ef2765125eca",
                    "postId": "5c31919c598d1440c5af15c2",
                    "createdAt": "2019-01-06T08:22:06.684Z",
                    "updatedAt": "2019-01-06T08:22:06.684Z"
                },
                {
                    "_id": "5c31bace4821eb66da070b84",
                    "userId": "5c2ce506ca46ef2765125eca",
                    "postId": "5c31919c598d1440c5af15c2",
                    "createdAt": "2019-01-06T08:22:38.366Z",
                    "updatedAt": "2019-01-06T08:22:38.366Z"
                },
                {
                    "_id": "5c31bada4821eb66da070b85",
                    "userId": "5c2ce506ca46ef2765125eca",
                    "postId": "5c31919c598d1440c5af15c2",
                    "createdAt": "2019-01-06T08:22:50.576Z",
                    "updatedAt": "2019-01-06T08:22:50.576Z"
                },
                {
                    "_id": "5c31bb6e4821eb66da070b87",
                    "userId": "5c2ce506ca46ef2765125eca",
                    "postId": "5c31919c598d1440c5af15c2",
                    "createdAt": "2019-01-06T08:25:18.823Z",
                    "updatedAt": "2019-01-06T08:25:18.823Z"
                },
                {
                    "_id": "5c31bb714821eb66da070b88",
                    "userId": "5c2ce506ca46ef2765125eca",
                    "postId": "5c31919c598d1440c5af15c2",
                    "createdAt": "2019-01-06T08:25:21.930Z",
                    "updatedAt": "2019-01-06T08:25:21.930Z"
                },
                {
                    "_id": "5c31bb824821eb66da070b89",
                    "userId": "5c2ce506ca46ef2765125eca",
                    "postId": "5c31919c598d1440c5af15c2",
                    "createdAt": "2019-01-06T08:25:38.200Z",
                    "updatedAt": "2019-01-06T08:25:38.200Z"
                },
                {
                    "_id": "5c31bb8d4821eb66da070b8a",
                    "userId": "5c2ce506ca46ef2765125eca",
                    "postId": "5c31919c598d1440c5af15c2",
                    "createdAt": "2019-01-06T08:25:49.285Z",
                    "updatedAt": "2019-01-06T08:25:49.285Z"
                },
                {
                    "_id": "5c31bb8f4821eb66da070b8b",
                    "userId": "5c2ce506ca46ef2765125eca",
                    "postId": "5c31919c598d1440c5af15c2",
                    "createdAt": "2019-01-06T08:25:51.560Z",
                    "updatedAt": "2019-01-06T08:25:51.560Z"
                },
                {
                    "_id": "5c31bd8b4821eb66da070b8d",
                    "userId": "5c2ce886ca46ef2765125ecb",
                    "postId": "5c31919c598d1440c5af15c2",
                    "createdAt": "2019-01-06T08:34:19.965Z",
                    "updatedAt": "2019-01-06T08:34:19.965Z"
                },
                {
                    "_id": "5c323b624821eb66da070b8e",
                    "userId": "5c2ce506ca46ef2765125eca",
                    "postId": "5c31919c598d1440c5af15c2",
                    "createdAt": "2019-01-06T17:31:14.062Z",
                    "updatedAt": "2019-01-06T17:31:14.062Z"
                }
            ],
            "comments": [],
            "shares": [],
            "id": "5c31919c598d1440c5af15c2"
        }

【问题讨论】:

  • 你能添加你的完整模型/模式吗?
  • 等待更新我的问题
  • 我更新了我的问题
  • 如果填充 likesCount 而不是 likes,会发生什么?

标签: node.js mongoose


【解决方案1】:

我刚刚通过将 Mongoose 版本从 v5.3.13 更新到 v5.4.3 解决了这个问题,现在它可以正常工作了, 不知道为什么它在以前的版本中不起作用。 也许该版本的 Mongoose 中没有 count 变量。

【讨论】:

    【解决方案2】:

    这会将计数输出为一个数字:

    await mongoose.connect(connectionString);
    await mongoose.connection.dropDatabase();
    
    const TextSchema = new Schema({
        testfield: String,
        post: {
            type: Schema.Types.ObjectId,
            ref: 'Post'
        },
    });
    
    const PostSchema = new Schema({
        locale: String
    }, {toObject: { virtuals: true }, timestamps: true});
    
    PostSchema.virtual('posts', {
        ref: 'Text',
        localField: '_id',
        foreignField: 'post',
        count: true,
    });
    
    let Text = mongoose.model('Text', TextSchema)
    let Post = mongoose.model('Post', PostSchema)
    
    const newPost = await Post.create({locale: 'something' });
    
    await Text.create([{ testField: 'test', post: newPost   }]);
    await Text.create([{ testField: 'test2',post: newPost,  }]);
    
    const posts = await Post.find({})
      .sort([['updatedAt', 'descending']])
      .populate('posts', '-__v -_id')
    
    console.log('POSTS', posts);
    

    这个的输出是这样的:

    POSTS { _id: 5c34d47468eb0f373564d4de,
            locale: 'something',
            createdAt: 2019-01-08T16:48:52.007Z,
            updatedAt: 2019-01-08T16:48:52.007Z,
            __v: 0,
            posts: 2,
            id: '5c34d47468eb0f373564d4de' }
    

    【讨论】:

    • 是的,我做的和我有问题的一样,但我得到的是likeCount的对象而不是likeCount PostSchema.virtual('likeCount', { ref: 'Like', localField: '_id', foreignField : 'postId', count: true });
    • 你用的是什么版本的mongodb和mongoose?
    • “猫鼬”:“^5.3.13”,“快递”:“^4.16.4”
    • 猫鼬版有问题吗?
    • 不,我想你应该没问题。请为您的问题创建一个最小的独立演示并将其发布在此处。
    猜你喜欢
    • 2020-11-11
    • 1970-01-01
    • 2020-08-01
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多