如何使用Node.js和Express在MongoDB服务中创建树状模式?

huangapple go评论89阅读模式
英文:

How can I make tree schema in MongoDB service using Node js and express

问题

我想在Angular 8中创建一个mat-tree,并且数据应该来自MongoDB数据库。为此,我需要在后端服务器中创建一个树形结构的模式,然后将数据放入该模式并从该模式中检索数据。到目前为止,我已经从本地数据创建了mat-tree,并在我的后端服务器中创建了一个树模型,如下所示:

const mongoose = require('mongoose');

const Schema = mongoose.Schema

const treeSchema = new Schema({
  name: String,
  parent: {
    type: Schema.Types.ObjectId,
    ref: 'Node'
  },
  children: [{
    type: Schema.Types.ObjectId,
    ref: 'Node'
  }],
  ancestors: [{
    type: Schema.Types.ObjectId,
    ref: 'Node'
  }]
})

module.exports = mongoose.model('Node', treeSchema)

我不知道如何使用API服务将数据放入和检索数据从这个模式中。任何帮助将不胜感激。谢谢!

英文:

I want to create a mat-tree in angular 8 and data should come from the MongoDB database. For this need, I have to make a tree-structured schema in my backend server and then put data in that schema and retrieve it from that schema. Till now I have created mat-tree from local data and made a tree model in my backend server which is as follow:

const mongoose = require('mongoose');

const Schema = mongoose.Schema

const treeSchema = new Schema({
  name: String,
  parent: {
    type: Schema.Types.ObjectId,
    ref: 'Node'
  },
  children: [{
    type: Schema.Types.ObjectId,
    ref: 'Node'
  }],
  ancestors: [{
    type: Schema.Types.ObjectId,
    ref: 'Node'
  }]
})

module.exports = mongoose.model('Node', treeSchema)

I don't know how to put and retrieve data from this schema using API service.
any help would be appreciated.
Thanks!

答案1

得分: 2

database mongodb and node js server with express

我找到了使用文档:

https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/mongoose

英文:

database mongodb and node js server with express

i have found to use document

https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/mongoose

huangapple
  • 本文由 发表于 2020年1月7日 00:18:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定