连接两个集合并在Mongoose中对它们进行排序
Join two collections and sort them in Mongoose
我有两个独立的集合,假设news
和pictures
。两个集合都有一个creationDate
字段,该字段保存记录创建时间的datetime
。
我想要的是从两个集合中选择最后10条记录,连接所有记录(现在列表有20条记录),然后按creationDate
字段对所有记录进行排序。猫鼬怎么可能呢?
你所要求的本质上是一个客户端操作,你真的只需要最多20个记录排序,所以这应该不是一个问题。
异步。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);
});
所以实际上你只是在"合并"两个数组,但是这样编码会简化事情。
一个端到端清单来澄清这里的用法。在运行如上所示的代码之前,这会为每个模型设置一些文档:
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' } ]
相关文章:
- JavaScript数组排序(函数)用于对表行进行排序,而不是排序
- 使用promise和mongoose对文档进行排序
- Selectize.js:如何对整数值的选项进行排序
- Javascript排序的图像弹出窗口..可以't单独弹出
- 如何通过引用var Using DataTables来进行分页或排序
- 为什么HTML5拖放的目标是孩子?(可排序列表)
- 如何缩短MongoDB ObjectId并在Mongoose模式中使用它
- AngularJs对所有页面中的所有记录进行排序
- 当属性不一致时,如何根据属性对JS对象列表进行排序
- 按从高到低对多个int变量进行排序
- jQuery UI可排序-多连接列表拖动
- Javascript排序字符串或数字
- 2个backbone.js集合,具有相同的模型,但排序顺序不同
- mongoose.connect undefined不是一个函数
- populate中的Mongoose排序不起作用
- Mongoose,按填充字段对查询进行排序
- 如何使用 Mongoose.js 3.8.20 和 MongoDB 2.6.5 对文档进行排序
- 如何在Mongoose上使用变量而不是字符串按数组索引进行排序
- 如何将Mongoose排序查询偏移到特定文档之后启动
- 连接两个集合并在Mongoose中对它们进行排序