【问题标题】:Is it possible to retrieve values from mongoDB during the creation of a schema?在创建模式期间是否可以从 mongoDB 中检索值?
【发布时间】:2022-01-23 13:31:11
【问题描述】:

我对 Nodejs 和 MongoDB 完全陌生,所以可能有一个非常简单的答案可用,但我很难找到它。

我想要做的是有一个值表,我可以在创建模式时检索它,以便如果这些值中的任何一个发生变化,我所要做的就是更新数据库而不编写任何代码。例如,假设我有一桌水果:

  • 苹果
  • 香蕉
  • 橙子

我想在定义我的架构时从该表中提取值,而不必执行以下操作:

var fruitSchema = new mongooseSchema({
   fruit: {
        type: String,
        enum : ['Apples','Bananas', 'Oranges'],
    },
})

在我看来,它看起来像这样:

var fruitSchema = new mongooseSchema({
   fruit: {
        type: String,
        enum : [{A database call}],
    },
})

或者可能:

const myFruit = {database call}

var fruitSchema = new mongooseSchema({
   fruit: {
        type: String,
        enum : myFruit,
    },
})

A) 这样的事情可能吗? B) 在实施此类事情方面,我应该注意哪些最佳做法?

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    您可以做的是运行原始 mongodb 查询。示例:

    mongoose.connection.db.collection('fruitCollection').find({ ... })
    

    从集合和基于它的构建模式/模型中获取您的预期数据。

    【讨论】:

      猜你喜欢
      • 2012-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-13
      • 2017-05-24
      • 2011-05-29
      • 2020-12-06
      • 1970-01-01
      相关资源
      最近更新 更多