簡體   English   中英

Mongoose 聚合時忽略空字段

[英]Mongoose ignoring empty fields while aggregating

收藏:A

文檔:

{
color: '',
age: 22,
name: 'John'
}

收藏:B

文檔:

{
name:'John',
marks: 100
}

我通過加入 collections 這樣的東西來執行聚合

A.aggregate([
    {
      $match: filters
    },
    {
      $lookup: {
        from: 'B',
        localField: 'name',
        foreignField: 'name',
        as: 'alias1',
      }
    },
    {
      $unwind: '$alias1'
    },
    {
      $project: {
        color: '$color',
        marks: '$alias1.marks',
       }
}])

我得到的結果是:

{
marks:100
}

我期待的結果是:

{
color: '',
marks: 100
}

我假設由於顏色字段為空,默認情況下聚合會忽略它。

基本上,我想連接兩個表並獲取幾個字段的數據。 如果聚合不能解決這個問題,那么任何其他實現預期結果的方法都會有很大幫助。 謝謝。

嘗試以下查詢:

A.aggregate([
    {
      $match: filters
    },
    {
      $lookup: {
        from: 'B',
        localField: 'name',
        foreignField: 'name',
        as: 'alias1',
      }
    },
    {
      $unwind: '$alias1'
    },
    {
      $project: {
        color: { $ifNull: [ "$color", "" ] },
        marks: '$alias1.marks',
       }
}])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM