How to insert bulk queries in prisma from real backend? - javascript

So I am looking through the Prisma docs and I come across an example to create a relational query. This query inserts a new post and assigns it an author with an existing category.
const assignCategories = await prisma.post.create({
data: {
title: 'How to be Bob',
categories: {
create: [
{
assignedBy: 'Bob',
assignedAt: new Date(),
category: {
connect: {
id: 9,
},
},
},
{
assignedBy: 'Bob',
assignedAt: new Date(),
category: {
connect: {
id: 22,
},
},
},
],
},
},
})
I can understand what this query does but I don't understand how to implement this on a backend with the incoming request body.
Suppose I have this request body
{
"title": "how to be bob",
"categories": [
{
"assignedby": "bob",
"category": {
"id": 9
}
},
{
"assignedby": "bob",
"category": {
"id": 22
}
}
]
}
How do I transform this request body to the data object in the first codeblock?

I got it. It was in my face all along. Just use .map to map through the categories
const data = {
title: 'how to be bob',
categories: [
{
assignedby: 'bob',
category: {
id: 9,
},
},
{
assignedby: 'bob',
category: {
id: 22,
},
},
],
};
const mappedData = {
title: data.title,
categories: {
create: data.categories.map((i) => ({
assignedBy: i.assignedby,
assignedAt: new Date(),
category: {
connect: {
id: i.category.id,
},
},
})),
},
};
console.log(mappedData);
which logs this
"title": "how to be bob",
"categories": {
"create": [
{
"assignedBy": "bob",
"assignedAt": "2021-10-24T12:10:00.397Z",
"category": {
"connect": {
"id": 9
}
}
},
{
"assignedBy": "bob",
"assignedAt": "2021-10-24T12:10:00.397Z",
"category": {
"connect": {
"id": 22
}
}
}
]
}
}
Just what we exactly need.

Related

merge and remove elements in nested arrays

i have this array, i want to merge all elements inside the objects in the nested arrays and remove the duplicates..
the array is the output of mongo db populate so answers from there or just js will be amazing :)
"visitors": [
[
{
"name": "matan",
"id": "61793e6a0e08cdcaf213c0b1"
},
{
"name": "shani",
"id": "61793e910e08cdcaf213c0b5"
}
],
[
{
"name": "david",
"id": "6179869cb4944c6b19b05a23"
},
{
"name": "orit",
"id": "617986e535fdf4942ef659bd"
}
],
[
{
"name": "david",
"id": "6179869cb4944c6b19b05a23"
},
{
"name": "orit",
"id": "617986e535fdf4942ef659bd"
}
]
]
would like this output -
"visitors": [
{
"name": "matan",
"id": "61793e6a0e08cdcaf213c0b1"
},
{
"name": "shani",
"id": "61793e910e08cdcaf213c0b5"
},
{
"name": "david",
"id": "6179869cb4944c6b19b05a23"
},
{
"name": "orit",
"id": "617986e535fdf4942ef659bd"
},
]
these are my collections
i need to get all visitors on one solar system,
so > solars > planets > visitors
const solarsModel = new Schema({
planets: [ { type: Schema.Types.ObjectId ,ref:'planet'} ],
starName: { type: String, required: true, default: "" }
})
const planetModel = new Schema({
planetName: { type: String, required: true, default: "" },
system:{type: Schema.Types.ObjectId, ref: 'solar'},
visitors: [{ type: Schema.Types.ObjectId , ref: 'visitor'}]
})
const visitorModel = new Schema({
visitorName:{ type: String, required: true, default: "" },
homePlanet: {type: Schema.Types.ObjectId, ref:"planet" },
visitedPlanets: [{ type: Schema.Types.ObjectId, ref:"planet" }]
})
this is what i did to achieve a result would love to use Aggregate..
const response = await solarModel
.findById({ _id: data.id })
.select({ starName: 1, _id: 0 })
.populate({
path: "planets",
select: { visitors: 1, _id: 0 },
populate: {
path: "visitors",
select: "visitorName",
},
})
.exec();
solved with this
exports.findVisitorSystemHandler = async (data) => {
const systemName = await solarModel.findById({ _id: data.id });
const response = await planetModel.aggregate([
{ $match: { system: makeObjectId(data.id) } },
{
$lookup: {
from: "visitors",
localField: "visitors",
foreignField: "_id",
as: "solarVisitors",
},
},
{
$project: {
solarVisitors: {
visitedPlanets: 0,
homePlanet: 0,
__v: 0,
},
},
},
{ $unwind: "$solarVisitors" },
{
$group: {
_id: null,
system: { $addToSet: systemName.starName },
solarVisitors: {
$addToSet: {
id: "$solarVisitors._id",
name: "$solarVisitors.visitorName",
},
},
},
},
{ $unwind: "$system" },
{
$project: {
_id: 0,
},
},
]);
return response;
};
You can use aggregate() like this:
$unwind twice due to nested array
$group using $addToSet to not get duplicates.
db.collection.aggregate([
{
"$unwind": "$visitors"
},
{
"$unwind": "$visitors"
},
{
"$group": {
"_id": null,
"visitors": {
"$addToSet": {
"id": "$visitors.id",
"name": "$visitors.name"
}
}
}
}
])
Example here
(1) Flatten the array of arrays
visitors = visitors.flat();
Which gives us this:
[
{ name: 'matan', id: '61793e6a0e08cdcaf213c0b1' },
{ name: 'shani', id: '61793e910e08cdcaf213c0b5' },
{ name: 'david', id: '6179869cb4944c6b19b05a23' },
{ name: 'orit', id: '617986e535fdf4942ef659bd' },
{ name: 'david', id: '6179869cb4944c6b19b05a23' },
{ name: 'orit', id: '617986e535fdf4942ef659bd' }
]
(2) Get unique ids
let uniqueIds= [...new Set(visitors.map(v => v.id)]
Which gives us this:
[
'61793e6a0e08cdcaf213c0b1',
'61793e910e08cdcaf213c0b5',
'6179869cb4944c6b19b05a23',
'617986e535fdf4942ef659bd'
]
(3) Get new list of visitors based only on uniqueIds
visitors = uniqueIds.map(id => {
let name = visitors.find(v => v.id === id).name;
return {
id,
name
}
});
Which gives us this:
[
{ name: 'matan', id: '61793e6a0e08cdcaf213c0b1' },
{ name: 'shani', id: '61793e910e08cdcaf213c0b5' },
{ name: 'david', id: '6179869cb4944c6b19b05a23' },
{ name: 'orit', id: '617986e535fdf4942ef659bd' },
]
Query
reduce with concat to flatten
union with an empty array,just to remove duplicates
if you have other fields except visitors they are not affected
PlayMongo
aggregate(
[{"$set":
{"visitors":
{"$setUnion":
[{"$reduce":
{"input": "$visitors",
"initialValue": [],
"in": {"$concatArrays": ["$$value", "$$this"]}}},
[]]}}}])
Results
[{
"visitors": [
{
"name": "david",
"id": "6179869cb4944c6b19b05a23"
},
{
"name": "matan",
"id": "61793e6a0e08cdcaf213c0b1"
},
{
"name": "orit",
"id": "617986e535fdf4942ef659bd"
},
{
"name": "shani",
"id": "61793e910e08cdcaf213c0b5"
}
]
}]

How know the value of property of object in javascript?

I have this object:
var x= {
"data": {
"getLand": {
"id": "xxx",
"bid": [{
"result": "ON",
"buyer": {
"username": "Dis"
},
"offerSet": [{
"createdStr": "202",
"value": 1
}]
},
{
"result": "CANCEL",
"buyer": {
"username": "Dis"
},
"offerSet": [{
"createdStr": "202",
"value": 15
}]
}
]
}
}
}
How can i know is result === "ON" && username == "Dis" ?
I tried with this:
for (var key in x.data.getLand.bid) {
if((x.data.getLand.bid[key].result === 'ON') && (x.data.getLand.bid[key].buyer.username.toUpperCase() === 'DIS')){
console.log(x.data.getLand.bid[key]);
}
}
it gives me some problems .... sometimes it works and sometimes it doesn't. Would you be kind enough to show me another way?
You can utilize the ES6 array function forEach to loop through the array items.
const x = { data: { getLand: { id: "xxx", bid: [ { result: "ON", buyer: { username: "Dis", }, offerSet: [ { createdStr: "202", value: 1, }, ], }, { result: "CANCEL", buyer: { username: "Dis", }, offerSet: [ { createdStr: "202", value: 15, }, ], }, ], }, }, }
const bidList = x.data.getLand.bid
bidList.forEach((bid, index) => {
if (bid.result === 'ON' && bid.buyer.username.toUpperCase() === 'DIS') console.log(index, bid)
})
You can do this with filter and forEach loop.
like this:
var x = { data: { getLand: { id: "xxx", bid: [ { result: "ON", buyer: { username: "Dis" }, offerSet: [{ createdStr: "202", value: 1 }] }, { result: "CANCEL", buyer: { username: "Dis" }, offerSet: [{ createdStr: "202", value: 15 }] } ] } } };
x.data.getLand.bid
.filter(
({ result, buyer: { username } }) => result === "ON" || username === "Dis"
)
.forEach((el, id) => console.log(el, id));
An example with for...of
var x = { data: { getLand: { id: "xxx", bid: [ { result: "ON", buyer: { username: "Dis", }, offerSet: [ { createdStr: "202", value: 1, }, ], }, { result: "CANCEL", buyer: { username: "Dis", }, offerSet: [ { createdStr: "202", value: 15, }, ], }, ], }, }, };
for (const item of x.data.getLand.bid) {
if (item.result === "ON" && item.buyer.username.toUpperCase() === "DIS") {
console.log(item);
}
}
Edit:
If you need the index, you can use forEach.
var x = { data: { getLand: { id: "xxx", bid: [ { result: "ON", buyer: { username: "Dis", }, offerSet: [ { createdStr: "202", value: 1, }, ], }, { result: "CANCEL", buyer: { username: "Dis", }, offerSet: [ { createdStr: "202", value: 15, }, ], }, ], }, }, };
x.data.getLand.bid.forEach((item, i) => {
if (item.result === "ON" && item.buyer.username.toUpperCase() === "DIS") {
console.log(i);
console.log(item);
}
});

Two relational Json array merge and convert to treeview JStree

I tried to two seperate ajax query JSON object list and get json list, like this:
listperson = [
{
Id: 25,
name: "person1"
},
{
Id: 26,
name: "person2"
}
];
listPersonDetails=
[
{
personId:25,
lecture: "math",
score:80
},
{
personId:25,
lecture: "chm",
score:95
},
{
personId:26,
lecture: "math",
score:60
}
]
And then I tried to display treeview; but I couldnt convert it . How can I merge and convert to treeview like this in jstree:
----person1
-person1 info 1
-person1 info 2
----person2
-person2 info 1
-person2 info 2
Try like this:
var listperson = [{
Id: 25,
name: 'person1'
},
{
Id: 26,
name: 'person2'
}
];
var listPersonDetails = [{
personId: 25,
lecture: 'math',
score: 80
},
{
personId: 25,
lecture: 'chm',
score: 95
},
{
personId: 26,
lecture: 'math',
score: 60
}
]
var jstreedata = [];
//convert to the necessary JSON format
$.each(listperson, function(indexperson, person) {
jstreedata.push({
'text': person.name,
'state': {
'opened': true,
'selected': false
},
'children': []
});
$.each(listPersonDetails, function(indexdetails, details) {
if (person.Id == details.personId) {
jstreedata[indexperson]['children'].push({
'text': details.lecture,
'li_attr': {
'title': 'Score = ' + details.score
}
})
};
});
});
//create jsTree
$(function() {
$('#jstree_demo_div').jstree({
'core': {
'data': jstreedata
}
});
});
<link rel="stylesheet" href="dist/themes/default/style.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>
<div id="jstree_demo_div"></div>
Loop through both arrays and add the combined result to a new array in the jsTree JSON format. The output looks like this:
[
{
"text": "person1",
"state": {
"opened": true,
"selected": false
},
"children": [
{
"text": "math",
"li_attr": {
"title": "Score = 80"
}
},
{
"text": "chm",
"li_attr": {
"title": "Score = 95"
}
}
]
},
{
"text": "person2",
"state": {
"opened": true,
"selected": false
},
"children": [
{
"text": "math",
"li_attr": {
"title": "Score = 60"
}
}
]
}
]

Javascript Object Tranformation

I am using the map to transforming the JSON. Instead of Single object, I am getting an Array of data.
Snipt Shown Below.
I am trying to achieve following JSON
{
"data": {
"test1": {
"abc": {
"size": "big",
"id": "1"
}
},
"test2": {
"abc": {
"size": "small",
"id": "2"
}
}
}
}
But getting following JSON
{
"data": [{
"test1": {
"abc": {
"size": "big",
"id": "1"
}
}
}, {
"test2": {
"abc": {
"size": "big",
"id": "2"
}
}
}]
}
Here is my code.
const test = [{ id: 'ddec9c7f-95aa-4d07-a45a-dcdea96309a9',
ad_id: 'test1',
country: 'ID',
abc: { size: 'big', id: '1' },
},
{ id: 'ddec9c7f-95aa-4d07-a45a-dcdea96309a9',
ad_id: 'test2',
country: 'ID',
abc: { size: 'small', id: '2' },
},
];
const transformedTest = test.map(result =>
({ [result.ad_id]: { abc: result.abc } }));
const data = { data: transformedTest };
console.log(JSON.stringify(data));
Any help will be appreciated
Try this:
const test = [
{
"id": "ddec9c7f-95aa-4d07-a45a-dcdea96309a9",
"ad_id": "test1",
"country": "ID",
"abc": {
"size": "big",
"id": "1"
}
},
{
"id": "ddec9c7f-95aa-4d07-a45a-dcdea96309a9",
"ad_id": "test2",
"country": "ID",
"abc": {
"size": "big",
"id": "2"
}
}
];
const result = test.reduce((acc,{ad_id, abc})=> (acc.data[ad_id] = {abc}, acc),{data:{}})
console.log(result);
const transformedTest = test.reduce((pv, result) =>
({...pv, [result.ad_id]: { abc: result.abc } }), {});
You could take Object.fromEntries and map new key/value pairs and get a new object from it.
const
test = [{ id: 'ddec9c7f-95aa-4d07-a45a-dcdea96309a9', ad_id: 'test1', country: 'ID', abc: { size: 'big', id: '1' } }, { id: 'ddec9c7f-95aa-4d07-a45a-dcdea96309a9', ad_id: 'test2', country: 'ID', abc: { size: 'small', id: '2' } }];
transformedTest = Object.fromEntries(test.map(({ ad_id, abc} ) => [ad_id, { abc }]));
data = { data: transformedTest };
console.log(data);
Loop through the array using a simple for...of loop and create a data object. Use Shorthand property names to create the abc nesting:
const test=[{id:'ddec9c7f-95aa-4d07-a45a-dcdea96309a9',ad_id:'test1',country:'ID',abc:{size:'big',id:'1'},},{id:'ddec9c7f-95aa-4d07-a45a-dcdea96309a9',ad_id:'test2',country:'ID',abc:{size:'small',id:'2'}}]
const data = {}
for(const { ad_id, abc } of test) {
data[ad_id] = { abc }
}
console.log({ data })
.as-console-wrapper { min-height: 100%; }
The function map returns an array rather than a specific key-value object, an alternative is using the function reduce to build the desired output.
const test = [{ id: 'ddec9c7f-95aa-4d07-a45a-dcdea96309a9', ad_id: 'test1', country: 'ID', abc: { size: 'big', id: '1' },},{ id: 'ddec9c7f-95aa-4d07-a45a-dcdea96309a9', ad_id: 'test2', country: 'ID', abc: { size: 'big', id: '2' },}],
data = { data: test.reduce((a, result) =>
({...a, [result.ad_id]: { abc: result.abc } }), Object.create(null))};
console.log(JSON.stringify(data, null, 2));
.as-console-wrapper { min-height: 100%; }

object.save() not a function error

I believe the issue revolves around the use of .save() and could possibly be associated with the object not having a .save() method.
/ This is the debugger output (thought I might as well clarify) /
Debugger listening on port 15454
Server started
events.js:141
throw er; // Unhandled 'error' event
^
TypeError: examBoard.save is not a function
at /home/ubuntu/workspace/tests/seeds.js:168:23
at Immediate.Model.$wrapCallback (/home/ubuntu/workspace/tests/node_modules/mongoose/lib/model.js:3336:16)
at Immediate._onImmediate (/home/ubuntu/workspace/tests/node_modules/mongoose/node_modules/mquery/lib/utils.js:137:16)
at processImmediate [as _immediateCallback] (timers.js:383:17)
Process exited with code: 1
Just to help anyone find the actual issue, I have used examBoad.save(); twice (once after the creation of all the examBoards and once close to the end of the program) and question.save(); once, I believe the problem to be with examBoard.save();
var mongoose = require("mongoose");
var examBoard = require("./models/examBoard");
var question = require("./models/question");
var user = require("./models/user");
function seedDB() {
examBoard.remove({}, function(err) {
if (err) {
console.log("Could not remove examBoard data" + err);
} else {
examBoard.insertMany({
name: "AQA",
modules: [{
name: "a",
topics: [{
name: "a1",
}, {
name: "a2",
}, {
name: "a3",
}]
}, {
name: "b",
topics: [{
name: "b1",
}, {
name: "b2",
}, {
name: "b3",
}]
}, {
name: "c",
topics: [{
name: "c1",
}, {
name: "c2",
}, {
name: "c3",
}]
}]
}, {
name: "OCR",
modules: [{
name: "a",
topics: [{
name: "a1",
}, {
name: "a2",
}, {
name: "a3",
}]
}, {
name: "b",
topics: [{
name: "b1",
}, {
name: "b2",
}, {
name: "b3",
}]
}, {
name: "c",
topics: [{
name: "c1",
}, {
name: "c2",
}, {
name: "c3",
}]
}]
}, {
name: "EXL",
modules: [{
name: "a",
topics: [{
name: "a1",
}, {
name: "a2",
}, {
name: "a3",
}]
}, {
name: "b",
topics: [{
name: "b1",
}, {
name: "b2",
}, {
name: "b3",
}]
}, {
name: "c",
topics: [{
name: "c1",
}, {
name: "c2",
}, {
name: "c3",
}]
}]
});
examBoard.save();
question.insertMany({
content: "<p>This is a question</p><br><img src='https://hd.unsplash.com/photo-1469537053566-3081fd1e0de2'><br><p>This is a bit after the image</p>",
mark: 6,
methods: [
[{
mark: 1,
content: "1a"
}, {
mark: 2,
content: "1b"
}, {
mark: 3,
content: "1c"
}],
[{
mark: 1,
content: "2a"
}, {
mark: 2,
content: "2b"
}, {
mark: 3,
content: "2c"
}],
[{
mark: 1,
content: "3a"
}, {
mark: 2,
content: "3b"
}, {
mark: 3,
content: "3c"
}]
]
}, {
content: "<p>This is a question</p><br><img src='https://hd.unsplash.com/photo-1465311440653-ba9b1d9b0f5b'><br><p>This is a bit after the image</p>",
mark: 9,
methods: [
[{
mark: 2,
content: "1a"
}, {
mark: 3,
content: "1b"
}, {
mark: 4,
content: "1c"
}],
[{
mark: 2,
content: "2a"
}, {
mark: 3,
content: "2b"
}, {
mark: 4,
content: "2c"
}],
[{
mark: 2,
content: "3a"
}, {
mark: 3,
content: "3b"
}, {
mark: 4,
content: "3c"
}]
]
}, {
content: "<p>This is a question</p><br><img src='https://hd.unsplash.com/photo-1467404899198-ccadbcd96b91'><br><p>This is a bit after the image</p>",
mark: 12,
methods: [
[{
mark: 3,
content: "1a"
}, {
mark: 4,
content: "1b"
}, {
mark: 5,
content: "1c"
}],
[{
mark: 3,
content: "2a"
}, {
mark: 4,
content: "2b"
}, {
mark: 5,
content: "2c"
}],
[{
mark: 3,
content: "3a"
}, {
mark: 4,
content: "3b"
}, {
mark: 5,
content: "3c"
}]
]
});
question.save();
examBoard[0].module[0].topic[0].push(question[0]);
examBoard[0].module[0].topic[0].push(question[1]);
examBoard[0].module[0].topic[0].push(question[2]);
examBoard[0].module[0].topic[1].push(question[0]);
examBoard[0].module[0].topic[1].push(question[1]);
examBoard[0].module[0].topic[1].push(question[2]);
examBoard[0].module[0].topic[2].push(question[0]);
examBoard[0].module[0].topic[2].push(question[1]);
examBoard[0].module[0].topic[2].push(question[2]);
examBoard[0].module[1].topic[0].push(question[0]);
examBoard[0].module[1].topic[0].push(question[1]);
examBoard[0].module[1].topic[0].push(question[2]);
examBoard[0].module[1].topic[1].push(question[0]);
examBoard[0].module[1].topic[1].push(question[1]);
examBoard[0].module[1].topic[1].push(question[2]);
examBoard[0].module[1].topic[2].push(question[0]);
examBoard[0].module[1].topic[2].push(question[1]);
examBoard[0].module[1].topic[2].push(question[2]);
examBoard[0].module[2].topic[0].push(question[0]);
examBoard[0].module[2].topic[0].push(question[1]);
examBoard[0].module[2].topic[0].push(question[2]);
examBoard[0].module[2].topic[1].push(question[0]);
examBoard[0].module[2].topic[1].push(question[1]);
examBoard[0].module[2].topic[1].push(question[2]);
examBoard[0].module[2].topic[2].push(question[0]);
examBoard[0].module[2].topic[2].push(question[1]);
examBoard[0].module[2].topic[2].push(question[2]);
examBoard[0].module[0].topic[0].push(question[0]);
examBoard[0].module[0].topic[0].push(question[1]);
examBoard[0].module[0].topic[0].push(question[2]);
examBoard[0].module[0].topic[1].push(question[0]);
examBoard[0].module[0].topic[1].push(question[1]);
examBoard[0].module[0].topic[1].push(question[2]);
examBoard[0].module[0].topic[2].push(question[0]);
examBoard[0].module[0].topic[2].push(question[1]);
examBoard[0].module[0].topic[2].push(question[2]);
examBoard[0].module[1].topic[0].push(question[0]);
examBoard[0].module[1].topic[0].push(question[1]);
examBoard[0].module[1].topic[0].push(question[2]);
examBoard[0].module[1].topic[1].push(question[0]);
examBoard[0].module[1].topic[1].push(question[1]);
examBoard[0].module[1].topic[1].push(question[2]);
examBoard[0].module[1].topic[2].push(question[0]);
examBoard[0].module[1].topic[2].push(question[1]);
examBoard[0].module[1].topic[2].push(question[2]);
examBoard[0].module[2].topic[0].push(question[0]);
examBoard[0].module[2].topic[0].push(question[1]);
examBoard[0].module[2].topic[0].push(question[2]);
examBoard[0].module[2].topic[1].push(question[0]);
examBoard[0].module[2].topic[1].push(question[1]);
examBoard[0].module[2].topic[1].push(question[2]);
examBoard[0].module[2].topic[2].push(question[0]);
examBoard[0].module[2].topic[2].push(question[1]);
examBoard[0].module[2].topic[2].push(question[2]);
examBoard[1].module[0].topic[0].push(question[0]);
examBoard[1].module[0].topic[0].push(question[1]);
examBoard[1].module[0].topic[0].push(question[2]);
examBoard[1].module[0].topic[1].push(question[0]);
examBoard[1].module[0].topic[1].push(question[1]);
examBoard[1].module[0].topic[1].push(question[2]);
examBoard[1].module[0].topic[2].push(question[0]);
examBoard[1].module[0].topic[2].push(question[1]);
examBoard[1].module[0].topic[2].push(question[2]);
examBoard[1].module[1].topic[0].push(question[0]);
examBoard[1].module[1].topic[0].push(question[1]);
examBoard[1].module[1].topic[0].push(question[2]);
examBoard[1].module[1].topic[1].push(question[0]);
examBoard[1].module[1].topic[1].push(question[1]);
examBoard[1].module[1].topic[1].push(question[2]);
examBoard[1].module[1].topic[2].push(question[0]);
examBoard[1].module[1].topic[2].push(question[1]);
examBoard[1].module[1].topic[2].push(question[2]);
examBoard[1].module[2].topic[0].push(question[0]);
examBoard[1].module[2].topic[0].push(question[1]);
examBoard[1].module[2].topic[0].push(question[2]);
examBoard[1].module[2].topic[1].push(question[0]);
examBoard[1].module[2].topic[1].push(question[1]);
examBoard[1].module[2].topic[1].push(question[2]);
examBoard[1].module[2].topic[2].push(question[0]);
examBoard[1].module[2].topic[2].push(question[1]);
examBoard[1].module[2].topic[2].push(question[2]);
examBoard[2].module[0].topic[0].push(question[0]);
examBoard[2].module[0].topic[0].push(question[1]);
examBoard[2].module[0].topic[0].push(question[2]);
examBoard[2].module[0].topic[1].push(question[0]);
examBoard[2].module[0].topic[1].push(question[1]);
examBoard[2].module[0].topic[1].push(question[2]);
examBoard[2].module[0].topic[2].push(question[0]);
examBoard[2].module[0].topic[2].push(question[1]);
examBoard[2].module[0].topic[2].push(question[2]);
examBoard[2].module[1].topic[0].push(question[0]);
examBoard[2].module[1].topic[0].push(question[1]);
examBoard[2].module[1].topic[0].push(question[2]);
examBoard[2].module[1].topic[1].push(question[0]);
examBoard[2].module[1].topic[1].push(question[1]);
examBoard[2].module[1].topic[1].push(question[2]);
examBoard[2].module[1].topic[2].push(question[0]);
examBoard[2].module[1].topic[2].push(question[1]);
examBoard[2].module[1].topic[2].push(question[2]);
examBoard[2].module[2].topic[0].push(question[0]);
examBoard[2].module[2].topic[0].push(question[1]);
examBoard[2].module[2].topic[0].push(question[2]);
examBoard[2].module[2].topic[1].push(question[0]);
examBoard[2].module[2].topic[1].push(question[1]);
examBoard[2].module[2].topic[1].push(question[2]);
examBoard[2].module[2].topic[2].push(question[0]);
examBoard[2].module[2].topic[2].push(question[1]);
examBoard[2].module[2].topic[2].push(question[2]);
examBoard.save();
console.log("Done seeding");
}
});
}
module.exports = seedDB;
The examBoard model
var mongoose = require("mongoose");
var topicSchema = new mongoose.Schema({
name: String,
questions: [{
type: mongoose.Schema.Types.ObjectId,
ref: "question"
}],
});
var moduleSchema = new mongoose.Schema({
name: String,
topics: [topicSchema]
});
var examBoardSchema = new mongoose.Schema({
name: String,
modules: [moduleSchema]
});
module.exports = mongoose.model("examBoard", examBoardSchema);
require implements a singleton pattern, in mongoose when require a model, it's necessary to instance a new object, something like this:
var exam = new examBoard(request.body); // request.body can to contain the values of the schema structure
exam.save();
The save method are only on the instance of an model, example
var examboard = new examBoard();
examboard.property = 'something';
examboard.save();
actually you already saving the objects when you say examBoard.insertMany();

Categories

Resources