javascript - Issue in creating another collection from result of a mongodb query in node -


i trying create collection result of query in mongodb using nodejs

the code working fine if have few number of records. not working if there large records.

error

[error: document exceeds maximum allowed bson size of 16777216 bytes] 

code

mongoclient.connect(mongodb_uri, function (err, database) {     if (err) throw err;     coll = database.collection("smmc_queue");     coll.insert({         "starttime": moment().format("dd-mm-yyyy, h:mm:ss a"),         "casename": req.body.casename,         "author": req.session.user,         "jobtype": 'query',         "status": 'in progress',         "searchdescription": searchdescription,         "query": searchcondition,         "sort": sortcondition     }, function (err, sucessdoc) {         res.redirect(302, '/querystatus');         mongoclient.connect(mongodb_uri, function (err, database) {             if (err) throw err;             collection = database.collection(req.body.casename);             collection.find(searchobj, sortobj).toarray(function (err, doc) {                 var ncn = 'query_' + sucessdoc[0]._id.tostring();                 if (!err) {                     coll.update({                         "_id": sucessdoc[0]._id                     }, {                         $set: {                             "endtime": moment().format("dd-mm-yyyy, h:mm:ss a"),                             status: "completed",                         }                     }, {                         upsert: true,                         multi: true                     }, function (err, result) {});                     resultcollection = database.collection(ncn);                     console.log(typeof doc)                     console.log(doc.length)                     resultcollection.insert(doc, function (err, res) {                         console.log(err);                     });                 } else {                     coll.update({                         "_id": sucessdoc[0]._id                     }, {                         $set: {                             "endtime": moment().format("dd-mm-yyyy, h:mm:ss a"),                             status: "completed",                             "result": err                         }                     }, {                         upsert: true,                         multi: true                     }, function (err, result) {});                 }             });         });     }); }) 

console samples

search query : { cardno: 821 } typeof doc : object doc.length : 756 err (insert resultcollection) : null  search query : { iodate: { '$gt': mon apr 02 2012 00:00:00 gmt+0530 (ist) } } typeof doc : object doc.length : 374010 err (insert resultcollection) : [error: document exceeds maximum allowed bson size of 16777216 bytes] 

where did go wrong?

the reason fails because when inserting doc value array, of "lengths" reporting.

while array of documents fine issue insert, not "individual" document size in question, whole insert. breaking maximum bson size, because "request" insert larger maximum (16mb).

what need break up, try amounts of 500 @ time.

var last = 0; ( var i=0; < doc.length; i+= 500 ) {     resultcollection.insert( doc.slice(last, i) );     last = i; } resultcollection.insert( doc.slice(last, doc.length) ); 

or possibly better that.

but generally, break array. can limit (16mb) per request.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -