简体   繁体   中英

Mongoose: populate method doesn't properly work

I could use some help here, I can't figure out why the populate method doesn't work as expected. I want to model some "rooms" with people inside them.

var PersonSchema = new Schema({
    _id   : Schema.ObjectId,
    name  : String,
});
var Person = mongoose.model('Person', PersonSchema, 'Person');

var RoomSchema = new Schema({
      _id    : Schema.ObjectId, 
      name : String,
      People : [{ type: Schema.ObjectId, ref: 'Person' }],
});
var Room = mongoose.model('Room', RoomSchema, 'Room');

So, I have a Person collection with one entry:

{ 
"name" : "person1", 
"_id" : ObjectId("4f9e5bb68bcd69fb0c000002") 
}

and a Room collection with one entry:

{ 
"_id" : ObjectId("4f9e5bb68bcd69fb0c000001"), 
"name" : "room1", 
"People" : [ ObjectId("4f9e5bb68bcd69fb0c000002") ] 
}

When I try to execute this query:

Room.findOne({ 'name': 'room1' }).populate('People').run(function (err, result) {
  if(err) console.log(err);
  console.log(result);
});

I get as output:

{ _id: 4f9e5bb68bcd69fb0c000001,
  name: 'room1',
  People: []
}

Why doesn't Mongoose populate the people field? I just don't understand what could be wrong.

This problem was a bug in 2.x version of mongoose. It has been fixed: https://github.com/LearnBoost/mongoose/issues/877

Because mongoose doesn't 'join' data (which is what I think you're trying to do).

What you want to do is something like this (note this is with Comments and blogpost)

Schema

var Comments = new Schema({
    title     : String
  , body      : String
  , date      : Date
});

var BlogPost = new Schema({
    author    : ObjectId
  , title     : String
  , body      : String
  , buf       : Buffer
  , date      : Date
  , comments  : [Comments]
  , meta      : {
      votes : Number
    , favs  : Number
  }
});

Then create a blog model and set the comments (person in a room in your example)

// retrieve my model
var BlogPost = mongoose.model('BlogPost');

// create a blog post
var post = new BlogPost();

// create a comment
post.comments.push({ title: 'My comment' });

post.save(function (err) {
  if (!err) console.log('Success!');
});

If you dont want to do the embedded documents, you can do a search from that callback. For example, this is my coffee-script example of exactly this getting user name from a tweets author:

ModelTweets.find {}, (err, tweetResults) ->
    if tweetResults.length == 0
      res.render 'tweets/index', tweets: 0
    else
      # Get the Author for each tweet
      tweetResults.forEach (tweetInfo) ->
        count++
        ModelUser.findOne {'_id': tweetInfo.author}, (err, userInfo) ->
          tweets.push {id: tweetInfo._id, tweet: tweetInfo.tweet, created_at: tweetInfo.created_at, author: userInfo.username}

        if count == tweetResults.length
          res.render 'tweets/index', {tweets: tweets || {}}

That should get you on the right track :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM