简体   繁体   English

如何在Mongoose中合并两个集合并使用createdDate对它们进行排序

[英]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: 我看到两种方法:

  1. Easy way, but not the best way: If you, say, need to show 30 items on a page you need: 简单方法,但不是最佳方法:例如,如果您需要在页面上显示30个项目,则需要:
    • Load 30 items from Ringtone, load 30 items from AlbumSchema 从铃声中加载30个项目,从AlbumSchema中加载30个项目
    • Put them together in one array 将它们组合成一个阵列
    • Do final sort in memory. 在内存中进行最终排序。

Downside of this approach is you need to load more data into memory than needed and also perform in memory sort manually. 这种方法的缺点是您需要将更多的数据加载到内存中,并且需要手动执行内存排序。

  1. A better way. 更好的方法。 If you need to show data from two collections it can be a good sign to have one collection instead if two. 如果您需要显示两个集合中的数据,那么拥有一个集合而不是两个集合可能是一个好兆头。 Your two collections schema looks very similar. 您的两个集合架构看起来非常相似。 Later you can use sparse indexes to optimize queries which require only one of two entities. 稍后,您可以使用稀疏索引来优化仅需要两个实体之一的查询。

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 如果选择第一种方式,则实现细节:您可以使用asyncpromises并行地从两个查询中加载数据,并使用下划线进行最终的内存排序

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM