[英]How to find key with type objectId , ref to <some model>
I have created a Notes model having schema as shown below我创建了一个 Notes model,其架构如下所示
const notesschema = new Schema({
user :{
type : Schema.Types.ObjectId,
ref : 'User',
required : true
},
problem : {
type : Schema.Types.ObjectId,
ref : 'Problems',
required : true
},
content : {
type : 'string'
}
},{
timestamps : true
})
To show the User his notes for a particular task/problem I am trying to fetch notes and show to him and possibly update if he do some changes and save, The problem is with this schema I dont know how to write <model.findById >API to find notes from my notes model having particular user and specific task/problem.Which I would know the Id of.为了向用户展示他对特定任务/问题的笔记,我正在尝试获取笔记并向他展示,如果他做了一些更改并保存,可能会更新,问题在于这个模式我不知道如何写 <model.findById >从我的笔记 model 中查找具有特定用户和特定任务/问题的笔记的 API。我会知道它的 ID。
With this particular schema, and my current knowledge i would have to write So much code.有了这个特定的模式,以及我目前的知识,我将不得不编写这么多代码。 So if there is any easier way to do this task is welcomed,
因此,如果有任何更简单的方法来完成这项任务,欢迎,
I was also thinking to change my schema and just placing my user id in my schema instead of whole user and finding notes from my database我也在考虑改变我的架构,只是将我的用户 ID 放在我的架构中而不是整个用户并从我的数据库中查找注释
const notesschemaOfUser = await notesschema.findOne({user: user_id});
You create the notes' collection the same way you're doing it,你创建笔记的集合的方式和你做的一样,
const notesSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User', // # the name of the user model
required: true
},
problem: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Problem', // # the name of the user model
required: true
},
content: String
})
Then you'll create the model of the notesSchema as follows:然后,您将创建 notesSchema 的 model,如下所示:
const NoteModel = mongoose.model('Note', notesSchema, 'notes')
export them so you can use them in your controllers:导出它们以便您可以在控制器中使用它们:
module.exports = {
NoteModel,
notesSchema
}
or if you're using +es6 modules (think of, if you're using TypeScript):或者如果您使用的是 +es6 模块(想想,如果您使用的是 TypeScript):
export default {
NoteModel,
notesSchema
}
This will result in creating the following table ( collection ) in the database:这将导致在数据库中创建下表(集合):
Let's think of the following challenges:让我们考虑以下挑战:
To get all the notes:要获取所有注释:
NoteModel.find({})
To get all the users:获取所有用户:
UserModel.find({}) // you should have something like this in your code of course
To get all the problems:得到所有的问题:
ProblemModel.find({}) // you should have something like this in your code of course
To get all the notes of a user:获取用户的所有笔记:
NotesModel.find({ user: USER_ID })
To search for notes by problem s :要按问题搜索笔记:
NotesModel.find({ problem: PROBLEM_ID })
Now, the above is how you do it in mongoose, now let's create a RESTFUL controller for all of that: (assuming you're using express)现在,以上是您在 mongoose 中的操作方式,现在让我们为所有这些创建一个 RESTFUL controller:(假设您使用的是 express)
const expressAsyncHandler = require('express-async-handler') // download this from npm if you want
app.route('/notes').get(expressAsyncHandler(async (req, res, next) => {
const data = await NotesModel.find(req.query)
res.status(200).json({
status: 'success',
data,
})
}))
The req.query
is what's going to include the search filters, the search filters will be sent by the client (the front-end) as follows: req.query
将包含搜索过滤器,搜索过滤器将由客户端(前端)发送,如下所示:
http://YOUR_HOST:YOUR_PORT/notes?user=TheUserId
http://YOUR_HOST:YOUR_PORT/notes?problem=TheProblemId
http://YOUR_HOST:YOUR_PORT/notes?content=SomeNotes
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.