Model.create does not work on very large array of documents - node.js

I am facing a problem with mongoose Model.create method.
When i call
Model.create(arrayOfThousandDocs, function(err){});
After 15 min (sufficient for all the docs to get saved) when i switch to mongo shell and query upon total no of docs saved
then i find only something around 700-800 (no of docs saved varies every time Model.create is called).
And mongoose or mongo returns no any error.
Have anyone faced the same bug?
Please tell me how to resolve it.

it may be because, Model.create uses forEach method, and that is not suitable in nodejs asynchronous mode programming.. Please correct me if if i am wrong..
and suggest your views..
here is the source : http://mongoosejs.com/docs/api.html#model_Model.create

Related

Unable to use Code data type in Mongoose Schema

I wanted to store a data type of Code in my MongoDB database per the docs.
The issue is that when I use Mongoose to specify this type, I get an error:
ReferenceError: Code is not defined
And yes, Mongoose's doc's don't have a type of Code.
Say I want to save Javascript code in my MongoDB database, how could I do so?
For more insight into why I am doing this: I want to save Puppeteer scripts in the database, and using the FS module add them to a file, execute them and send the results as a response object back to the client.
Right now, the code is saved as String, which seems to cause execution errors.
For more information, please don't hesitate to ask.
Thank you,

mongoose findById would not work with correct Id

I was creating a database storing user information. Previously, the system worked well, after I performed npm update of mongoose, the system failed and I dont know why. The following is the error message I encountered.
CastError: Cast to ObjectId failed for value "xxxxxxxxxxxxxxx" at path "_id" for model "User"
Then, I write some simple testing code to test if the database is connected successfully. For example:
User.findOne({firstName: "David"}, function(err, user){console.log(user.lastName)});
The above worked perfectly.
I then found the corresponding Id from the database directly and create variable like:
var testId = "abcde"
and tried the following code:
User.findById(testId, function(err, user){console.log(user.firstName)});
Then it just output the error messages.
I have tried mongoose.Types.ObjectId.isValid('abcde') (abcde is just example) and it returned true. Therefore, I really have no ideas why it did not work...is it a mongoose bug?
Please help if you know the answer, thank you very much!
I just solved the problem.
As I mentioned, the findById function did not work after I updated mongoose. I was struggling on the code for few days and when I woke up this morning, I suddenly thought of upgrading my node.js.
That worked.
After upgrading node.js from v4.x.x to latest version, everything worked fine : )
This taught me a lesson that I should not upgrade a single module alone and care of compatibility issue.

I can´t find right values querying nested arrays with mongodb, mongoose and nodejs

Dear stackoverflow community,
I usually no make posts here, but in this time i ask myself why not?, after 2 hours without getting a solution.
This one is pretty basic but involves a specific json model, mongoose, mongodb and nodejs.
Now the problem, is that i´m trying to get the data for the user_ppal_id : 1 and the exam_design_id : 1 , the search of the user_ppal_id succeeded, but the exam_design_id that is into a nested array always returns to me the value 1 and 2 (and i´m passing by parameter the exam_design_id 1)...
This is my model, My nodejs rest method and how i´m using mongoose, nodejs and mongodb, My moongose model, A better view of the model from Umongo Ide:
SEE THE IMAGE HERE BECAUSE I AM NEW IN STACKOVERFLOW
All the structure is working, but the data from result is not the one i expected. You can check the response of this rest service by just clicking this url: http://207.244.75.230:8000/dyntestreports/getExamReport?idUserPpal=1&idExamDesign=1
In the response i´m getting the exam_design_id data for values 1 and 2 when i pass just the value 1.
Actually i tried with the following mongo queries and i not succeed:
1) ExamReportByUser.find({'user_ppal_id':userPpalId,'exams_created': {$elemMatch:{'exam_design_id': {$eq:examDesignId}}}}
2)ExamReportByUser.find({'user_ppal_id':userPpalId,'exams_created.0.exam_design_id':examDesignId}
At this moment i don´t know what i´m doing wrong even from a mongo console query i can´t get it works for just exam_design_id = 1 data.
I´m sure it could be a simple error but i´m not seeing it at this moment.
I really hope you can help me guys. Thanks in advance! Have a nice day.
My bad! I just have found the solution... And i post it here for anyone that had the same doubt... The code querying the data should be like this (it was just keys syntax, basic mongo):
ExamReportByUser.find({'user_ppal_id':userPpalId},{'exams_created': {$elemMatch:{'exam_design_id': examDesignId}}},'id user_ppal_id exams_created exam_design_id user_submitted_exam',function(err, result) { if (err) return console.log(err); return res.send(result); }); };
Now i´m getting the data of user 1 and exam_design 1.
I will not select this as an answer, waiting for the specialists ;)

Understanding mongoose findOne().remove()

As you know, in mongoose, we can remove all users with age 30 like this:
User.find({age: 30}).remove(callback);
Now, replace find() with findOne(), and I think it should remove only 1 user:
User.findOne({age: 30}).remove(callback);
oh, not as I expected, the code above also remove ALL instead of ONE
So, why findOne().remove() remove ALL instead of ONE? Is that a bug or a feature and why?
Thanks in advance!
P/S: I know findOneAndRemove() would remove one user for me, but in this question I want to understand findOne().remove()
I have reported this question to mongoose team, and got a reply:
https://github.com/LearnBoost/mongoose/issues/1851#issuecomment-31355346
Here's the message from aheckmann
"that's a good catch. findOne just sets the command name to run, remove() changes it back to a rice command but no limit was ever set. We should probably change that in 3.9 so that findOne sets
the limit as well."
Both find and findOne returns mongoose Query objects which only contains information about the model and the specified query. It's not taking into account findOne which is applied first in the callback. What you expect to happen is to have options be set like this User.findOne({age: 30}, null, {limit: 1}).remove() as this would only remove one and you could argue that this is a bug, but that depends on the usage. Like you have already pointed out, the right way to go is to use findOneAndRemove().
I'm kind of a noob but wouldn't you need to put your remove in the callback because this is an asynchronous function? Try something like:
User.findOne({age: 30}, function(err, user){
user.remove()
})

Collection name in Mongoose

Why would a database named 'blog' not allow a record insert and also give no return error and why would a database named 'blogs' allow a record inserts and return errors?
I just spent several hours going through all my code thinking I did something wrong. I have written many mongoose connected apps but when using the following it would return success but not insert the record and return no error as to why:
mongooose.connect('mongodb://localhost:27017/blog');
After banging my head against a wall for a bit I decided to change the database name:
mongooose.connect('mongodb://localhost:27017/blogs');
It works! But why would this name convention matter? I can't find anything in the documentation for MongoDB or Mongoosejs.
So I'm fairly certain mongodb doesn't care about database name "blog" vs "blogs". However, do note that mongoose has the questionably-helpful feature of silently queueing up operations while the database connection is still not established and then firing them off if/when the database connection is ready. That could be causing your confusion. To test that theory, pass a callback to mongoose.connect and put a console.log in the callback so you know exactly when the connection is ready.

Resources