Last Updated
Viewed 21 Times

Hi I've a project of online quiz portal web site. These are my models:

User model

const userSchema = new Schema({
  firstName: { type: String, required: true, maxlength: 15 },
  lastName: { type: String, required: true, maxlength: 15 },
  rank: Number

Score model

const scoreSchema = new Schema({
  userId: ObjectId,
  quizId: ObjectId,
  marks: Number,
  answers: [{ type: Object }],

I want to update my User model every time after I insert a record to the Score model.

This is unsurprisingly my formula for the rank.

avg = Total marks / Total participated quizzes

How could I achieve this using MongoDB or Mongoose?

Thanks in advanced.

I have some 40000 records For which I need to update a planId field and copy the records to same collection. Right Now I am using the basic

linkModel.find({},callback) & linkModel.insertMany([linksArray],callback)

But it takes up a lot of time. I have tried the aggregation but $out operator replaces the existing collection whereas I want to update it not replace.

The three steps I have tried so far:

db.links.aggregate([ { $match: { planId: ObjectId("5732f50a6886e5421259e823") } }, { $out: "temp" } ]);



But even copyTo method is slow. Is there a faster approach for this?

Is there a possibility in Mongodb to update a document, based on a query for 'count' of docs in other collection? I'm looking for options to make it an atomic operation.

To be more specific, the following is how the two collections are designed:


_id Name isReviewed [Boolean]

Reviews (Collection)

_id bookID [_id in Books Collection] Comments

I came up with this design as "Reviews" array for a book will keep growing and are mutable. Now, there's a requirement to set isReviewed flag in "Book" document to "true" when there's a "Review" doc. created for respective Book. The flag will stay "true" as long as there's at least one associated review existing for a book.

The same flag will be set to "false", when there're no reviews existing for a book. (The default value for the flag is "false" when the Book doc. is created) When a Review is deleted, count of reviews for a book is calculated to see if it can be set to "false". (if count is 0, then set to false).

This system is designed for a multi-user environment sharing all resources such as books and reviews. All Users are given read/write permissions on all resources.(I know it sounds weird to allow all users to be able to create/ read/ edit/ delete all reviews, But, Let's say that's the case functionally).

Now, considering the above case, how can I ensure I perform an update related to setting the "isReviewed" flag based on the 'count' in the 'Reviews' collection?

Is this a case which can't be solved without transactions (I mean, do I need RDBMS)? I'm open to redesign my collections as well.

Any help is appreciated, Thank you

An example of a document from a collection named categories in my MongoDB Database is:

    "_id": "Cookbooks",
    "parent": "Non-Fiction",
    "ancestors": [

All other entries in this category collection have this same schema and there are hundreds of them. What I want to do is insert an additional "All" element at the first position of the ancestors array to all those 100 entries/documents. for eg:

    "_id": "Cookbooks",
    "parent": "Non-Fiction",
    "ancestors": [

Is there a way that doesn't require writing a complete script and can be done just using the mongo shell or any other simple way? :/

Similar Question 5 (2 solutions) : Update large collection

Similar Question 6 (2 solutions) : update Collection record in mongo

Similar Question 7 (1 solutions) : Distinct collection in mongodb with _id field

Similar Question 8 (1 solutions) : What is Solr collection equivalent in mongodb?