2
\$\begingroup\$

I'm programming a model for a blog with authors and posts. I was following this doc: Mongoose Query Population

My concern is that the createPost function is inefficient. Another option is to nest the posts in the Author, but how I will get all posts? Should I use MySQL? Am I doing it right? (Take a look at the functions as well)

Some more details:

Pastebin

let mongoose = require("mongoose");
let Schema = mongoose.Schema;
let PostSchema = Schema({
 title: {type: String},
 body: {type: String},
 date: {type: Date},
 tags:[{type: String}],
 _author: {type: Schema.Types.ObjectId, ref: 'author'}
});
let AuthorSchema = Schema({
 name: {type: String},
 photo: {type: String},
 bio: {type: String},
 username: {type: String, index: true},
 posts:[{type: Schema.Types.ObjectId, ref: 'post'}],
 password: {type: String}
});
let Author = mongoose.model('author', AuthorSchema);
let Post = mongoose.model('post', PostSchema);
module.exports = Author;
module.exports = Post;
module.exports.createAuthor = (newAuthor, callback)=>{
 newAuthor.save(callback)
};
module.exports.createPost = (username, newPost, callback)=>{
 Author.findOne({username:username}).then((author)=>{
 newPost._author = author._id
 author.posts.push(newPost);
 newPost.save().then(err, auth)=>{
 author.save(callback);
 };
 },(err)=>{
 if(err)
 throw err;
 });
};
module.exports.getAuthorByPostTitle = (postTitle, callback)=>{
 Post.findOne({title:postTitle}).populate('_author').exec((err, post)=>{
 if(err)
 throw err;
 else
 return post._author;
 });
};
module.exports.getPostsByAuthorId = (authorId, callback)=>{
 Post.find({_author:authorId}).exec((err, posts)=>{
 if(err)
 throw err;
 else
 return posts;
 });
};
asked Jul 19, 2017 at 22:56
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

You can use the forEach function like this

const allPosts = [];
Author.find({}, function(err, users) {
 if (err) throw err;
 users.forEach(function(user) {
 const a = user.posts;
 a.forEach(us => allPosts.push(us));
 });
});

Now the allPosts array will have all the posts.

janos
113k15 gold badges154 silver badges396 bronze badges
answered Jan 3, 2018 at 9:01
\$\endgroup\$
3
  • \$\begingroup\$ It would have been better to edit your first answer, rather than posting a new. You can see the [edit] and [delete] links below the text of your posts. \$\endgroup\$ Commented Jan 3, 2018 at 12:06
  • \$\begingroup\$ Ok, that would solve the problem of querying all posts \$\endgroup\$ Commented Jan 3, 2018 at 23:36
  • \$\begingroup\$ What do you think about the createPost function? \$\endgroup\$ Commented Jan 3, 2018 at 23:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.