NoSQL Document-Oriented Databases : Project DB implementation with MongoDB
db.answers.insertOne({
question: ObjectId('...'),
student : ObjectId('...'),
exam: ObjectId('...'),
choices :[1,2]
})
Note: reference can be made by whatever field, not necessarily by the id fields
$lookup
operator allows to resolve references as nested documents in the results Example :
db.answers.aggregate({
$lookup: {
from: "questions",
localField: "question",
foreignField: "_id",
as: "questionData"
}
})
will resolve all the references coming from the questions
collection and place the reference in a field questionData
//create
db.questions.insertOne({
choices:[
{
choice:1,
choiceLabel:"abc",
valid:true
}
],
difficulty: 0,
subject:"",
topics:[]
});
db.questions.deleteOne({
_id: ObjectId('')
});
Note: you can put whatever criteria you want instead of id, be sure then to target only the documents you really want to delete, that's quite dangerous
//updateById
db.questions.updateOne(
{_id:ObjectId('')},
{$set: {subject:''}}
)
Note: you can put whatever criteria you want instead of id, be sure then to target only the documents you really want to update
//search by topics
db.questions.find({
topics: ['']
})