2016-04-24 116 views
1

我有有哪些需要填充路径的猫鼬模型:猫鼬排序的子文档填充

var adsSchema = new Schema({                 
    price:  { type: Number, min: 0, required: true }, 
    author:  { type: ObjectId, ref: 'Users', required: true }, 
    title:  { type: String, required: true }, 
    date:  { type: Date, default: Date.now }, 
    offers:  [{         
     price:  { type: Number, required: true },           
     date:  { type: Date, default: Date.now },           
     offerBy: { type: ObjectId, ref: 'Users', required: true }       
    }],                       
    category: { type: ObjectId },                
    images:  [{                    
     type: ObjectId,                   
     ref: 'Images'                   
    }],                       
    videos:  [String]                  
}); 

在一些GET请求,我需要填充的众多领域,因为我说的,特别是提供带有排序按'价格'按升序排列。

由于Mongoose的文档显示在那里(http://mongoosejs.com/docs/api.html#query_Query-populate),您可以按子路径进行排序。

我的问题是,事实并非如此。

还有就是我的代码,这样做是:

Ads.findOne({ _id: adId }) 
    .populate({ path: 'author', select: 'firstName lastName' }) 
    .populate('images') 
    .populate({ path: 'offers', options: { sort: { 'price': -1 } }) 
    .populate({ path: 'category', select: 'name' }) 
    .exec(function (err, ad) { 
    if (err) 
     deferred.reject({ status: 500, message: 'Database error.' }); 
    else if (!ad) 
     deferred.reject({ status: 500, message: 'The ad doesn\'t exist!.' }); 
    else { 
       deferred.resolve(ad.toJSON()); 
    } 
}) 

我已阅读尽可能多的问题/答案在这里或在猫鼬邮件列表,并提供:

我知道不可能从子文档结果排序文档结果,OK。但我只想对该子文档进行排序,只有这一个。这里有些反应似乎是说这是可能的:

我周围有2个问题:

  • 是否有可能(如猫鼬文档写入)在人口中排序一个子文档?
  • 它是否与我没有将ObjectId链接到其他架构的事实相关联?

谢谢您的回答;)

回答

0

就个人而言,我不使用“填入”尽可能我可以。过去我经历过很多麻烦。所以,我不知道如何在填充时进行排序。

您可以使用$ lookup方法进行聚合,而不用使用填充,您可以轻松地对任何字段进行排序。它与“人口”几乎相同。

Ads.aggregate([ 
     { 
      $lookup: { 
      from: 'Users', 
      localField: 'author', 
      foreignField: '_id', 
      as: 'authorName' 
      } 
     }, 
     { 
      $lookup: { 
      from: 'Images', 
      localField: 'images', 
      foreignField: '_id', 
      as: 'image' 
      } 
     }, 
     { 
      $lookup: { 
      from: 'categories', 
      localField: 'category', 
      foreignField: '_id', 
      as: 'category' 
      } 
     }, 
     { 
      $project: { 
      'authorName.firstName': 1, 
      'image.imagePath': 1, 
      'category.categoryName': 1, 
      '_id': 0 
      } 
     }, 
     { 
      $sort : { 'authorName.firstName' : -1} 
     } 

     ]).exec(function(err, adss) { 

     }); 

我没有正确地检查所有字段。请按照这种方式实现您的模型,并希望这可以给你一些想法。祝你好运

0

我找到了解决方案,这是我的第二个问题的回应。

当你在你的模式中写一个子文档就像我的问题一样,你不是在一个模型和另一个模型之间创建一个“关系”。这意味着填充方法根本不起作用。

您必须对ObjectId和与能够使用填充的关联模型进行引用,以及为您提供Mongoose的选项。这是一个例子:

var adsSchema = new Schema({ 
    price:  { type: Number, min: 0, required: true }, 
    author:  { type: ObjectId, ref: 'Users', required: true }, 
    offers:  [{ 
     type: ObjectId, 
     ref: 'Offers' 
    }], 
    videos:  [String] 
}); 

现在,.populate方法工作,因为猫鼬(等的MongoDB)可以上其他集合进行子查询和排序。事实并非如此,因为它在同一个模型中,所以Mongoose不会在同一个集合上执行子查询(因为我似乎理解它)。