How can I make a number id pattern 1, 2, 3, 4 in mongoose

佐手、 提交于 2021-01-29 09:00:25

问题


How do I make a pattern like this? I can't code that because i'm new to this.

This is my code

array = 1,2,3,4,5

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

});

Also using other modules if you ask.


回答1:


I hope it could be helpful

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 
}));

P.S. You could read answers here to be careful with ids: How do you implement an auto-incrementing primary ID in MongoDB?




回答2:


I hope this helps

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);


来源:https://stackoverflow.com/questions/62547933/how-can-i-make-a-number-id-pattern-1-2-3-4-in-mongoose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!