MongoDB查找字段不存在的所有文档,加上如果存在则应用字段运算符($ max)条件

$match我正在为我的聚合中的一个阶段寻找一个查询,它与这个问题几乎相同,但是..

  • 如果文档中不存在字段(在我的情况下命名为排名),则将文档添加到结果中

  • 但是如果字段存在,则将$operator条件(在我的情况下是$max)应用于该字段,并将所有符合此条件的文档添加到结果中。

MongoPlayground 与示例集合

结果应该是这样的:

[

  {

    "method": 3,

    "item": 1,

    "rank": 3 //because it has field named rank, and suits condition {rank: $max}

  },

  {

    "method": 4,

    "item": 1 //we need this, because document doesn't have rank field at all

  },

  {

    "method": 5,

    "item": 1 //we need this, because document doesn't have rank field at all

  }

]

我已经尝试过的事情:


            {

                $match: {

                    $or: [

                        {item: id, rank: {$exists: true, $max: "$rank"}}, //id === 1

                        {item: id, rank: {$exists: false}} //id === 1

                    ]

                }

            }

UPD:就目前而言,可能我不仅限于$match阶段,$project默认匹配后也相关,所以我可以在$match阶段请求每个文档,id无论是否有 docrank字段,然后在$project阶段做一个“按等级分离”$exists


慕沐林林
浏览 161回答 2
2回答

喵喔喔

试试这个:db.collection.aggregate([&nbsp; {&nbsp; &nbsp; $match: {&nbsp; &nbsp; &nbsp; item: id&nbsp; &nbsp; }&nbsp; },&nbsp; {&nbsp; &nbsp; $group: {&nbsp; &nbsp; &nbsp; _id: "$item",&nbsp; &nbsp;//<- Change here your searching field&nbsp; &nbsp; &nbsp; max: {&nbsp; &nbsp; &nbsp; &nbsp; $max: "$rank" //<- Change here your field to apply $max&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; $push: "$$ROOT"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; },&nbsp; {&nbsp; &nbsp; $unwind: "$data"&nbsp; },&nbsp; {&nbsp; &nbsp; $match: {&nbsp; &nbsp; &nbsp; $expr: {&nbsp; &nbsp; &nbsp; &nbsp; $or: [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $eq: [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $type: "$data.rank"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "missing"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $eq: [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "$data.rank",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "$max"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; },&nbsp; {&nbsp; &nbsp; $replaceWith: "$data"&nbsp; }])

慕神8447489

我找到了一个答案,与@Valijon 的方法分开,但它也是基于上面的逻辑。我的查询是:db.collection.aggregate([&nbsp; {&nbsp; &nbsp; $match: {&nbsp; &nbsp; &nbsp; item: id&nbsp; &nbsp; }&nbsp; },&nbsp; {&nbsp; &nbsp; $project: {&nbsp; &nbsp; &nbsp; method: 1,&nbsp; &nbsp; &nbsp; item: 1,&nbsp; &nbsp; &nbsp; rank: {&nbsp; &nbsp; &nbsp; &nbsp; $ifNull: [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "$rank",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; },&nbsp; {&nbsp; &nbsp; $group: {&nbsp; &nbsp; &nbsp; _id: "$item",&nbsp; &nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; $addToSet: "$$ROOT"&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; min_value: {&nbsp; &nbsp; &nbsp; &nbsp; $min: "$rank"&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; max_value: {&nbsp; &nbsp; &nbsp; &nbsp; $max: "$rank"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; },&nbsp; {&nbsp; &nbsp; $unwind: "$data"&nbsp; },&nbsp; {&nbsp; &nbsp; $match: {&nbsp; &nbsp; &nbsp; $or: [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $expr: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $eq: [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "$data.rank",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "$max_value"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $expr: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $eq: [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "$data.rank",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "$min_value"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; }&nbsp; }])我的查询基于$project给出空字段值 0 的阶段。它也可以是 -1,或任何未在集合中使用的值。然后我将结果分开。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript