Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recursive populate #710

Closed
unit51 opened this issue Feb 2, 2012 · 1 comment
Closed

recursive populate #710

unit51 opened this issue Feb 2, 2012 · 1 comment

Comments

@unit51
Copy link

unit51 commented Feb 2, 2012

Hello,

i have this Schema:

var ChatSchema = new Schema({
    title: String,
    founderUser : { type: Schema.ObjectId, ref: 'User'},
    members : [{ type: Schema.ObjectId, ref: 'User'}],
    messages : [{ type: Schema.ObjectId, ref: 'Message'}],
});

var MessageSchema = new Schema({
    sender: { type: Schema.ObjectId, ref: 'User' },
       date: { type: Date, default: Date.now },
    text: String,
});

now i want to query a chat:

Chat
 .findOne({ _id:obj._id})
 .populate('founderUser', ['username', '_id'])
 .populate('members', ['username', '_id'])
 .populate('messages', ['_id'], null, { limit: 10, sort: [['date', 'asc' ]] })

but i also want to populate the messages sender (username & _id)
Something like this:

 .populate('members.sender', ['username', '_id'])

would be very helpful.

This is my current solution (kinda ugly o_o )

Chat
.findOne({ _id: item.obj._id})
.populate('founderUser', ['username', '_id'])
.populate('members', ['username', '_id'])
.populate('messages', ['_id'], null, { limit: 10, sort: [['date', 'asc' ]] })
.run(function (err, chat) {
    Message
    .find({ _id: { $in : chat.messages } })
    .populate('sender', ['username', '_id'])
    .run(function (err, messages) {
        var jsonObject = chat.toObject();
        jsonObject.messages = messages; 

        callback(jsonObject);   
    })

});

i cant use embedded documents for the messages because i cannot filter the messages { limit: 10, sort: [['date', 'asc' ]] }

@aheckmann
Copy link
Collaborator

closing. tracking this in #601

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants