加入两个集合并在Mongoose中对它们进行排序

时间:2014-07-17 07:01:40

标签: javascript node.js mongodb mongoose

我有两个单独的集合,假设newspictures。两个集合都有一个creationDate字段,其中包含datetime个记录创建时间。

我想要的是从两个集合中选择最后10条记录,加入所有记录(现在列表有20条记录),然后按creationDate字段对所有记录进行排序。它是如何在Mongoose中实现的?

1 个答案:

答案 0 :(得分:1)

你所要求的实际上是一个客户端操作,你真的只想要最多20个记录进行排序,这应该不是问题。

async.concat方法应该对此有所帮助:

async.concat([News,Picture],function(model,callback) {
  // Get the last 10 results from each collection
  var query = model.find({}).sort({ "creationDate": -1 }).limit(10);
  query.exec(function(err,docs) {
    if (err) throw err;
    callback(err,docs);
  });
},
function(err,result) {
  if (err) throw err;
  // results are merged, now sort by date
  result = result.sort(function(a,b) {
    return (a.creationDate < b.creationDate) 
      ? 1 : (a.creationDate > b.creationDate) ? -1 : 0;
  });
  console.log(result);

});

所以你真的只是&#34;合并&#34;两个数组,但以这种方式编码简化了事情。


端到端列表以澄清此处的用法。这会在运行代码之前为每个模型设置一些文档,如上所示:

var async = require('async'),
    mongoose = require('mongoose'),
    Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/series');

var newsSchema = new Schema({
  type: { type: String, default: "News" },
  index: Number,
  creationDate: { type: Date, default: Date.now }
});

var pictureSchema = new Schema({
  type: { type: String, default: "Picture" },
  index: Number,
  creationDate: { type: Date, default: Date.now }
});

var News = mongoose.model( "News", newsSchema );

var Picture = mongoose.model( "Picture", pictureSchema );

async.series(
  [
    // Clean existing
    function(callback) {
      async.each([News,Picture],function(model,callback) {
        model.remove(function(err) {
          if (err) throw err;
          callback();
        });
      },function(err) {
        callback();
      });
    },

    // Insert 20 of each
    function(callback) {
      console.log("inserting");
      var count = 0;
      async.whilst(
        function() { return count < 20 },
        function(callback) {
          count++;
          async.eachSeries([News,Picture],function(model,callback) {
            var doc = new model({ index: count });
            setTimeout(function() {
              doc.save(function(err) {
                if (err) throw err;
                callback();
              });
            }, 20);
          },function(err) {
            callback();
          });
        },
        function(err) {
          callback();
        }
      );
    }
  ],
  function(err) {
    console.log("listing");

    // Get the last 10 of each
    async.concat([News,Picture],function(model,callback) {
      var query = model.find({}).sort({ "creationDate": -1 }).limit(10);
      query.exec(function(err,docs) {
        if (err) throw err;
        callback(err,docs);
      });
    },
    function(err,result) {
      if (err) throw err;

      // result is merged via "concat" now sort it.
      result = result.sort(function(a,b) {
        return (a.creationDate < b.creationDate)
          ? 1 : (a.creationDate > b.creationDate)
          ? -1 : 0;
      });
      console.log(result);
      mongoose.disconnect();
    });
  }
);

输出就在这些方面:

[ { _id: 53c79f4b7daf2d676ff0f185,
    index: 20,
    __v: 0,
    creationDate: Thu Jul 17 2014 20:02:51 GMT+1000 (EST),
    type: 'Picture' },
  { _id: 53c79f4b7daf2d676ff0f184,
    index: 20,
    __v: 0,
    creationDate: Thu Jul 17 2014 20:02:51 GMT+1000 (EST),
    type: 'News' },
  { _id: 53c79f4b7daf2d676ff0f183,
    index: 19,
    __v: 0,
    creationDate: Thu Jul 17 2014 20:02:51 GMT+1000 (EST),
    type: 'Picture' },
  { _id: 53c79f4b7daf2d676ff0f182,
    index: 19,
    __v: 0,
    creationDate: Thu Jul 17 2014 20:02:51 GMT+1000 (EST),
    type: 'News' },
  { _id: 53c79f4b7daf2d676ff0f181,
    index: 18,
    __v: 0,
    creationDate: Thu Jul 17 2014 20:02:51 GMT+1000 (EST),
    type: 'Picture' },
  { _id: 53c79f4b7daf2d676ff0f180,
    index: 18,
    __v: 0,
    creationDate: Thu Jul 17 2014 20:02:51 GMT+1000 (EST),
    type: 'News' },
  { _id: 53c79f4b7daf2d676ff0f17f,
    index: 17,
    __v: 0,
    creationDate: Thu Jul 17 2014 20:02:51 GMT+1000 (EST),
    type: 'Picture' },
  { _id: 53c79f4b7daf2d676ff0f17e,
    index: 17,
    __v: 0,
    creationDate: Thu Jul 17 2014 20:02:51 GMT+1000 (EST),
    type: 'News' },
  { _id: 53c79f4b7daf2d676ff0f17d,
    index: 16,
    __v: 0,
    creationDate: Thu Jul 17 2014 20:02:51 GMT+1000 (EST),
    type: 'Picture' },
  { _id: 53c79f4b7daf2d676ff0f17c,
    index: 16,
    __v: 0,
    creationDate: Thu Jul 17 2014 20:02:51 GMT+1000 (EST),
    type: 'News' } ]