【问题标题】:How to map an input field as different field in mongodb如何将输入字段映射为mongodb中的不同字段
【发布时间】:2020-12-10 17:16:04
【问题描述】:

假设我在 MongoDB 中插入数据

db.collection.insertMany([{username:'Jack',msg:'hello'},{username:'Jenny',msg:'hello world'},{username:'Rose',msg:'Hi howz You'}])

现在我希望这样插入数据:

{account_username:'Jack',msg:'hello'}
{account_username:'Jenny',msg:'hello world'}
{account_username:'Rose',msg:'Hi howz You'}

不是这个:

{username:'Jack',msg:'hello'}
{username:'Jenny',msg:'hello world'}
{username:'Rose',msg:'Hi howz You'}

我在我的 NodeJS 代码中使用 mongoose,我可以在 Schema 中做一些事情,所以只要输入是用户名,它就会自动引用我定义的 mongoose Schema 的 account_username。

【问题讨论】:

    标签: node.js mongodb mongoose mongodb-query mongoose-schema


    【解决方案1】:

    如果将 mongoose 与 mongoDB 一起使用,则可以使用 mongoose 虚函数功能轻松做到这一点,Mongoose Virtuals

    Mongoose 病毒具有 Setter 和 Getter,它们具有定义不同名称的列的功能

    【讨论】:

      猜你喜欢
      • 2018-03-24
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 2015-03-18
      • 1970-01-01
      • 2012-04-02
      • 2021-12-08
      • 2013-05-15
      相关资源
      最近更新 更多