【问题标题】:How can I make a number id pattern 1, 2, 3, 4 in mongoose如何在猫鼬中制作数字 id 模式 1、2、3、4
【发布时间】:2020-06-24 13:22:35
【问题描述】:

如何制作这样的图案?我无法编写代码,因为我是新手。

这是我的代码

array = 1,2,3,4,5

const note = new notedModel ({
      _id: array,
      note: args[1],

});

如果您要求,也可以使用其他模块。

【问题讨论】:

    标签: javascript node.js mongoose


    【解决方案1】:

    希望对你有帮助

    const array = [1,2,3,4,5];
    
    const notes = array.map(element => new notedModel({
        _id: element,
        note: args[1], // or maybe args[element] if you need to select arg by array's element 
    }));
    

    附:您可以在此处阅读答案以小心使用 id:How do you implement an auto-incrementing primary ID in MongoDB?

    【讨论】:

      【解决方案2】:

      希望对你有帮助

      class NotedModel{
        constructor(id, arg){
          this._id = id
          this._note = arg
        }
      }
      
      var arr = [1, 2, 3, 4, 5]
      var note = [];
      
      for(i = 0; i < arr.length; i++){
        note.push(new NotedModel(arr[i], i));
      }
      
      console.log(note);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-14
        • 1970-01-01
        • 2020-04-15
        • 2021-07-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多