1

For example suppose I have the following 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
  }
});

I know how to query BlogPost by comment attributes. How can I do a query such as getting the latest five comments? i.e. reverse sort all comments by ObjectId and then getting the first five results. Would I need to separate into its own Comments collection to do this?

4

1 に答える 1

5

Hopefully, someday we'll have virtual collections. But until then you have to move comments to a separate collection to be able to perform full array of queries on them.

Alternatively, you can use Aggregation Framework.

于 2012-05-28T13:22:19.903 に答える