Help for MEAN project

Can anyone help me with my project it’s based on MEAN stack and i have some difficulty in mongodb.Actually i want to use one of the field in my document as a variable in a loop in nodejs i am not able find it anywhere can anyone help me with it please it would be great help.

2 Likes

Hello @Jatin_Kadam, welcome to the community!

It would be helpful for others to read and respond to your question if you could describe your issue in more detail and elaborate on your doubts. With more information we can try to find an answer.

Cheers
Michael

2 Likes

Yes sure,So i have a field in my documents which has number so i want to use that as my variable in for loop in nodejs to get results accordingly.

Hi! If sounds like what you are asking is:

Let’s say you have field in your collection called maxCount. And you want a for loop something like this:

for (let step = 0; step < maxCount; step++) {
 // do something here
}

Is that what you mean? If not, can you provide an example so we can best tailor the answer to your need? Thanx!

1 Like

Thankyou for your response.But it’s not what i was looking for.I think i am not able to convey my problem properly ,i’ll explain it in detail it would be great if you can help.So we are trying to create a Question Paper generator project using MEAN stack and in that while accepting a question there is a field named “marks” associated with it.So what i was thinking is for eg. inorder to get a 20 marks question paper use this field in forloop to get questions of exactly 20 marks.Is this possible or should i consider a different approach for this.

1 Like

What’s the data type of the ‘marks’ field?

Can you show us some sample records? e.g. is it something like:

{ 
 title: "Question Paper 1",
 marks: 10
}
{ 
 title: "Question Paper 2",
 marks: 20
}
{ 
 title: "Question Paper 3",
 marks: 15
}

and you want to return only “Question Paper 2”?

1 Like

.
This is a document in my collection which has a field maxMarks which i have to use so that the no. of documents i get has a total of 20 or any other marks that the user inputs.

@Jatin_Kadam Thank you, this is SUPER HELPFUL.

So, something like this?

// let's assume the object is stored in an object called 'document'
for (let step = 0; step < document.maxMarks; step++) {
 // do something here
}

or even:

// let's assume the object is stored in an object called 'document'
for (let step = document.minMarks; step < document.maxMarks; step++) {
 // do something here
}

Or is it more complex than that?

1 Like

Thankyou so much that was a great help.I want one more help as i shared a ScreenShot earlier of my document in that there is a filed subject i want to change it to “Multimedia System” from “MSD”
in all documents i tried aggregation and it shows the result but it does not commits to the change and is back to original can you please helpwith it.

1 Like

Hi @Jatin_Kadam - to rename a field in mongkdb, you can use the $rename operator like this:

db.collectionName.updateMany( {}, { $rename: { "Multimedia System": "MSD" }});

Yes,that worked .Thankyou!!