如果使用猫鼬不是空的,如何有条件地返回字段

发布于 2025-01-21 19:14:09 字数 795 浏览 0 评论 0原文

我有2个猫鼬模型,一种用于书籍,另一种用于作者。作者嵌入了书籍文档中

const mongoose = require("mongoose");

const BookSchema = mongoose.Schema({
    user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true
    },
    title: {
        type: String,
        minLength: 3,
        maxlength: 80,
        required: true
    },

,并查询我正在这样做的书籍

    async getBook(pid) {
        let book = await Book.findOne({
            _id: pid
        })
        .populate('user', 'name username')
        ;

        if (!book) {
            return false;
        }
        return book;
    }

,如预期的返回名称和作者的用户名。

但是,我想做的是如果用户名是空的,则返回名称,或者仅在用户名不为空时返回用户名。我该怎么做?

I have 2 mongoose models, one for books and one for authors. The author is embedded in the book document

const mongoose = require("mongoose");

const BookSchema = mongoose.Schema({
    user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true
    },
    title: {
        type: String,
        minLength: 3,
        maxlength: 80,
        required: true
    },

and to query the books I am doing this

    async getBook(pid) {
        let book = await Book.findOne({
            _id: pid
        })
        .populate('user', 'name username')
        ;

        if (!book) {
            return false;
        }
        return book;
    }

This works as expected returning name and username of the author.

However, what I would like to do is to return the name if username is empty, or only return the username if username is not empty. How can I do that please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掐死时间 2025-01-28 19:14:09

$ ifnull可以实现所需的行为。

db.Book.aggregate([
  {
    $match: {
      _id: {
        $in: [
          "b1",
          "b2",
          "b3"
        ]
      }
    }
  },
  {
    "$lookup": {
      "from": "User",
      "let": {
        user: "$user"
      },
      "pipeline": [
        {
          $match: {
            $expr: {
              $eq: [
                "$user",
                "$_id"
              ]
            }
          }
        },
        {
          $project: {
            _id: 0,
            name: {
              "$ifNull": [
                "$username",
                "$name"
              ]
            }
          }
        }
      ],
      "as": "user"
    }
  },
  {
    "$unwind": "$user"
  }
])

这是 mongo playground 供您参考。

The desired behaviour is achievable through $ifNull when wrangling the lookup subpipeline.

db.Book.aggregate([
  {
    $match: {
      _id: {
        $in: [
          "b1",
          "b2",
          "b3"
        ]
      }
    }
  },
  {
    "$lookup": {
      "from": "User",
      "let": {
        user: "$user"
      },
      "pipeline": [
        {
          $match: {
            $expr: {
              $eq: [
                "$user",
                "$_id"
              ]
            }
          }
        },
        {
          $project: {
            _id: 0,
            name: {
              "$ifNull": [
                "$username",
                "$name"
              ]
            }
          }
        }
      ],
      "as": "user"
    }
  },
  {
    "$unwind": "$user"
  }
])

Here is the Mongo playground for your reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文