[英]How to combine two collections in Mongoose and sort them using createdDate
I am developing application using MEAN.js in which I have two documents as below: 我正在使用MEAN.js开发应用程序,其中有两个文档,如下所示:
var RingtoneSchema = new Schema({
language: {
label: String,
code : String
},
deliver:{
type:Boolean,
default:false
},
artists: [RingtoneArtist],
digital_booklet:{
title: {
type: String,
trim:true
},
copyright:{
type: String,
trim:true
}
}
title: {
type: String,
default: '',
required: 'Please fill Album title',
trim: true
},
created: {
type: Date,
default: Date.now
}
});
var AlbumSchema = new Schema({
language: {
label: String,
code : String
},
deliver:{
type:Boolean,
default:false
},
artists: [artist],
title: {
type: String,
default: '',
required: 'Please fill Album title',
trim: true
},
created: {
type: Date,
default: Date.now
});
I want to get all the records from both schema and order them by created date to display them in client side. 我想从两个架构中获取所有记录,并按创建日期对它们进行排序以在客户端显示它们。 I don't know how to do this using mongoose.
我不知道怎么用猫鼬来做。 Any help will be appreciated.
任何帮助将不胜感激。
Mongodb itself is not relational database and any kind of "join" operation is not possible. Mongodb本身不是关系数据库,并且不可能进行任何类型的“连接”操作。 I see two ways to go:
我看到两种方法:
Downside of this approach is you need to load more data into memory than needed and also perform in memory sort manually. 这种方法的缺点是您需要将更多的数据加载到内存中,并且需要手动执行内存排序。
Implementation details if you pick first way: you can load data from two queries in parallel using async or using promises and do final in memory sort using underscore 如果选择第一种方式,则实现细节:您可以使用async或promises并行地从两个查询中加载数据,并使用下划线进行最终的内存排序
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.