【问题标题】:How we can perform sort on ObjectId column in mongodb using mongoose with expressjs-nodejs?我们如何使用带有 expressjs-nodejs 的 mongoose 对 mongodb 中的 ObjectId 列执行排序?
【发布时间】:2019-02-01 16:07:06
【问题描述】:

我们是一个 mongodb 点的结构,我想在这里描述一下。

我们正在使用 mongoose 5.4 并创建如下模型:

var userSchema = mongoose.Schema({  
id:{ type: Number, default: 1 },
first_name: String,
last_name: String,
mail: String,
password: String,
dob: { type: String, default: '' },
gender: { type: String, default: '' },
profile_photo: { type: String, default: '' },
ethnicity: { type: String, default: '' },
contact_number: { type: String, default: '' },
user_type: Number,
address1: { type: String, default: '' },
address2: { type: String, default: '' },
area: { type: String, default: '' },
city: { type: String, default: '' },
country: { type: String, default: '' },
postcode: { type: String, default: '' },
business_name: { type: String, default: '' }, 
ip_address: String,
status: Number,
tag_line: { type: String, default: '' },
is_influencer: Number,  
wallet_id: String,
token_balance: { type: Number, default: 0 },
point_balance: { type: Number, default: 0 },
badges: [String],
membership: { type: Schema.Types.ObjectId, ref: 'Membership' }, 
transaction: [{ type: Schema.Types.ObjectId, ref: 'Transaction' }], 
property: [{ type: Schema.Types.ObjectId, ref: 'Property' }],   
reviews: [{ type: Schema.Types.ObjectId, ref: 'Review' }],
created_date: String,
});

var User = mongoose.model('User', userSchema);

var propertySchema = mongoose.Schema({
id: Number,
property_name: String,
address1: String,
area: String,
post_code: String,
category: [{ type: Schema.Types.ObjectId, ref: 'Category' }], 
category_id: [Number],
property_desc: String,
property_images: String,
slug : String,
user_id: Number,
business_key: String,
user: { type: Schema.Types.ObjectId, ref: 'User' },
reviews: [{ type: Schema.Types.ObjectId, ref: 'Review' }],
status: Number,
is_claimed: Number,
created_date: String,
});

var Property = mongoose.model('Property', propertySchema);

var categorySchema = mongoose.Schema({  
id: Number,
category_name: String,
status: Number,
user: { type: Schema.Types.ObjectId, ref: 'User' },
property: [{ type: Schema.Types.ObjectId, ref: 'Property' }],   
created_date: String,
updated_date: String    
});

var Category = mongoose.model('Category', categorySchema);

我们也在这个项目中使用 async-await。我们正在使用以下方法获取位置以获取位置数据以及用户和类别。

sortClause = {};
sortClause.user=parseInt(-1);
let properties = await Property.find({'status':{$in:[1,2]}}).populate({path: 'user',
  model: 'User',select: 'first_name last_name mail contact_number'}).populate({path: 'category',
  model: 'Category',select: 'category_name id'}).sort(sortClause).skip(skip).limit(perpage).exec();

所以当我们在执行上述行之后在属性对象中获取数据时,我们正在获取属性,但是当我们按用户的属性模型列排序时,它确实正确地获取了数据。它按 objectID 排序,我们想获取用户的 first_name。

我已尝试按排序顺序指定 `{'user.first_name':-1},但它根本不起作用。

当我们对字符串或数字列进行排序时,它工作正常,但在这里我的预期结果会有点不同,这里我们想对 user.first_name 应用排序(在上面的示例中,用户是填充用户数据的 ObjectId 列,并且我想对用户的 first_name 列进行排序) 我们如何根据用户的 first_name 列获取属性?有没有针对这个问题的建议。

【问题讨论】:

  • @Shrabanee:很抱歉,但我正在寻找类似的但我的列数据类型是其他模型的 ObjectId,我想在我们完成后对该值的结果(ObjectId 的结果)进行排序填充()。
  • 你试过这个.populate({path: 'user', model: 'User',select: 'first_name last_name mail contact_number', options: { sort: { 'firstName': -1 } }})
  • @AnthonyWinzlet:我们已经使用上述方法进行了检查,但这并没有返回正确的结果。(在填充中使用排序时,我也删除了外部排序,但这也不起作用。)
  • 你得到了什么结果,你期望什么结果?请展示一些演示

标签: node.js mongodb express mongoose


【解决方案1】:

您可以尝试以下聚合从 mongodb 3.6 及以上

Property.aggregate([
  { "$match": { "status": { "$in": status }} },
  { "$lookup": {
    "from": "users",
    "let": { "user": "$user" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": [ "$_id", "$$user" ] } } },
      { "$project": { "first_name": 1, "last_name": 1, "mail": 1, "contact_number": 1 }}
    ],
    "as": "user"
  }},
  { "$lookup": {
    "from": "categories",
    "let": { "category": "$category" },
    "pipeline": [
      { "$match": { "$expr": { "$in": [ "$_id", "$$category" ] } } },
      { "$project": { "category_name": 1, "id": 1 }}
    ],
    "as": "category"
  }},
  { "$unwind": "user" },
  { "$sort": { "user.first_name": -1 }},
  { "$skip": skip },
  { "$limit": perpage }
])

【讨论】:

  • 工作正常,但对 Mongodb 3.4 版进行了切片更改。 let properties = await Property.aggregate([{$match : {"status": {"$in": status}} }, { $unwind: "$category"} , { $lookup: { from: "users", localField: "user", foreignField:"_id", as: "user"}}, { $lookup: { from: "categories", localField: "category", foreignField:"_id", as: "category"} } , { $match: { "category": { $ne: [] } }}]).collat​​ion({ locale: "en" }).sort({'user.first_name':-1}).limit(perpage ).exec();
  • 太棒了!!!但是使用 mongodb 3.6 版本的 $lookup 聚合有很多好处
猜你喜欢
  • 2021-10-02
  • 2019-01-24
  • 1970-01-01
  • 2018-08-13
  • 2023-03-27
  • 2021-08-29
  • 2015-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多