我正在构建具有喜欢,评论和分享功能的社交应用。所以我有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"
}
答案 0 :(得分:0)
这会将计数输出为数字:
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' }
答案 1 :(得分:0)
我刚刚解决了此问题,将Mongoose的版本从v5.3.13更新到v5.4.3,现在可以正常使用了, 不知道为什么它在以前的版本中不起作用。 也许count变量在该版本的Mongoose中不可用。