Last Updated
Viewed 06 Times
     

in mongoose when creating a connection, and setting poolSize option to say 10, does that mean we create 10 connections with this operation that will be handled my mongoose ? or how it works exactly ?

// With object options
mongoose.connect('mongodb://localhost:27017/myapp', {poolSize:10});

does executing the above code, means that we now have 10 connections that we can access using mongoose variable ? or how does it work exactly ?

I'm learning Mongoose. At the moment I did few nice things but I really don't understand exactly how Mongoose manage relationships between Schemas.

So, easy thing (I hope): I'm doing a classic exercise (by my self because I cannot find a good tutorial that create more than 2 Schemas) with 3 Schemas:

User, Post, Comment.

  • User can create many Post;
  • User can create many Comment;
  • Post belong to User.
  • Comment belong to User and Post.

I don't think it is something very hard uhu?

At the moment I can manage very well Relation between User and Post. My Unit test return exactly what I need, at the moment I'm using mongo-relation and I don't know if it is a good idea...

  it('Use should create a Post', function(done) {
    User.findOne({ email: 'test@email.com' }, function(err, user) {
      var post = {
        title: 'Post title',
        message: 'Post message',
        comments: []
      };
      user.posts.create(post, function(err, user, post) {
        if (err) return done(err);
        user.posts[0].should.equal(post._id);
        post.author.should.equal(user._id);
        // etc...
        done();
      });
    });
  });

The problem now is to create a comment. I can not create a comment that refere to the Post and to the User together.

I did something like that and works but when I perform a remove it is removed only from the Post and not from the User.

So I think there is something I miss or I still need to study to enhance it.

  it('User should add a Comment to a Post', function(done) {
    User.findOne({ email: 'test@email.com' }, function(err, user) {
      if (err) return done(err);

      var comment = new Comment({
        author: user._id,
        message: 'Post comment'
      });

      Post.findOne({ title: 'Post title'}, function(err, post) {
        if (err) return done(err);
        post.comments.append(comment, function(err, comment) {
          if (err) return done(err);

          post.save(function(err) {
            if (err) return done(err);
          });

          comment.author.should.equal(user._id);
          post.comments.should.have.length(1);
          // etc...
          done();
        });
      });
    });
  });

As you can see the code is not very "nice to see" but it works fine in terms of creations.

The problem is when I remove a Comment. It seems like something is wrong.

Here is the Model relationship:

// User Schema
var userSchema = new mongoose.Schema({
  // [...],

  posts: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Post' }],
  comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }],

});

// Post Schema
var postSchema = new mongoose.Schema({
    author: { type: mongoose.Schema.ObjectId, ref: 'User', refPath: 'posts' },
    title: String,
    message: String,
    comments: [{ type: mongoose.Schema.ObjectId, ref: 'Comment' }]
});

// Comment Schema
var commentSchema = new mongoose.Schema({
    author: { type: mongoose.Schema.ObjectId, ref: 'User', refPath: 'comments' },
    post: { type: mongoose.Schema.ObjectId, ref: 'Post', refPath: 'comments' },
    message: String
});

I really hope in your help to understand all this.

It will be nice also a simple good tutorial about it.

I am currently in the process of learning the MEAN stack, and in my studies I am currently on Mongoose.

I have an issue understanding what is the model below used for:

var campgroundSchema = new mongoose.Schema({
	name: String,
	image: String
});
var Campground = mongoose.model("Campground", campgroundSchema);

Why is it needed if the MongoDB itself is a NoSQL database? The only explanation I can think of is that it is made so that you can use the standard functions such as find() and create().

Are there other ways to do these things?

Also a MORE IMPORTANT QUESTION: What does the first argument ("Campground") in the code above refer to? I can't find it anywhere in the database.

I've read the documentation and I still don't understand it.

As you know, in mongoose, we can remove all users with age 30 like this:

User.find({age: 30}).remove(callback);

Now, replace find() with findOne(), and I think it should remove only 1 user:

User.findOne({age: 30}).remove(callback);

oh, not as I expected, the code above also remove ALL instead of ONE

So, why findOne().remove() remove ALL instead of ONE? Is that a bug or a feature and why?

Thanks in advance!

P/S: I know findOneAndRemove() would remove one user for me, but in this question I want to understand findOne().remove()

Similar Question 4 (1 solutions) : understanding mongoose [Schema.Types.Mixed]

Similar Question 6 (1 solutions) : Mongoose nextObject

Similar Question 7 (1 solutions) : Referencing for mongoose

Similar Question 8 (2 solutions) : Relationship mongoose

Similar Question 9 (1 solutions) : Mongoose models

cc