本文介绍了查找并排序外国馆藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个集合users,该集合中的每个文档以及其他属性在另一个集合:workouts中都有一个文档ID数组.集合workouts中的每个文档都有一个名为date的属性.

so I have a collection users, and each document in this collection, as well as other properties, has an array of ids of documents in the other collection: workouts.Every document in the collection workouts has a property named date.

这就是我想要得到的:

这是我的尝试,效果很好.

This is my attempt, which is working fine.

Users.aggregate([
    { 
        $match : {
            _id : ObjectId("whateverTheUserIdIs")
        }
    }, 
    { 
        $unwind : {
            path : "$workouts"
        }
    }, { 
        $lookup : {
            from : "workouts", 
            localField : "workouts", 
            foreignField : "_id", 
            as : "workoutDocumentsArray"
        }
    }, { 
        $project : {
            _id : false, 
            workoutData : {
                $arrayElemAt : [
                    $workoutDocumentsArray, 
                    0
                ]
            }
        }
    }, { 
        $project : {
            date : "$workoutData.date", 
            id : "$workoutData._id"
        }
    }, { 
        $sort : {date : -1}
    }
])

但是我拒绝相信我需要所有这些,因为在SQL中如此简单的查询!!我相信我至少必须能够将两个$project阶段合并为一个?但是我无法弄清楚如何看文档.

However I refuse to believe I need all this for what would be such a simple query in SQL!? I believe I must at least be able to merge the two $project stages into one? But I've not been able to figure out how looking at the docs.

提前感谢您抽出宝贵的时间! ;)

Thanks in advance for taking the time! ;)

====编辑-这是一些示例数据

====EDIT - This is some sample data

收藏用户:

[{
  _id:xxx,
  workouts: [2,4,6]
 },{
  _id: yyy,
  workouts: [1,3,5]
}]

大学体育锻炼:

[{
  _id:1,
  date: 1/1/1901
 },{
  _id:2,
  date: 2/2/1902
 },{
  _id:3,
  date: 3/3/1903
 },{
  _id:4,
  date: 4/4/1904
 },{
  _id:5,
  date: 5/5/1905
 },{
  _id:6,
  date: 6/6/1906
 }]

运行查询(例如,针对用户xxx的查询)后,我只想获取属于他的锻炼(其ID出现在他的锻炼数组中),所以我想要的结果如下所示:

And after running my query, for example for user xxx, I would like to get only the workouts that belong to him (whose ids appear in his workouts array), so the result I want would look like:

[{
  id:6,
  date: 6/6/1906
 },{
  id:4,
  date: 4/4/1904
 },{
  id:2,
  date: 2/2/1902
 }]

推荐答案

您不需要 $unwind workouts数组,因为它已经包含_id数组并使用 $replaceRoot 而不是执行"> $project

You don't need to $unwind the workouts array as it already contains array of _ids and use $replaceRoot instead of doing $project

Users.aggregate([
  { "$match": { "_id" : ObjectId("whateverTheUserIdIs") }}, 
  { "$lookup": {
    "from" : "workouts", 
    "localField" : "workouts", 
    "foreignField" : "_id", 
    "as" : "workoutDocumentsArray"
  }},
  { "$unwind": "$workoutDocumentsArray" },
  { "$replaceRoot": { "newRoot": "$workoutDocumentsArray" }}
  { "$sort" : { "date" : -1 }}
])

甚至使用新的 $lookup 语法

Users.aggregate([
  { "$match" : { "_id": ObjectId("whateverTheUserIdIs") }}, 
  { "$lookup" : {
    "from" : "workouts", 
    "let": { "workouts": "$workouts" },
    "pipeline": [
      { "$match": { "$expr": { "$in": ["$_id", "$$workouts"] }}},
      { "$sort" : { "date" : -1 }}
    ]
    "as" : "workoutDocumentsArray"
  }},
  { "$unwind": "$workoutDocumentsArray" },
  { "$replaceRoot": { "newRoot": "$workoutDocumentsArray" }}
])

这篇关于查找并排序外国馆藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 06:22