Mhad Morph
Mhad Morph

Reputation: 3

mongodb not insert the full array

I use the following to insert 499 entry into a subdocument, I get the data from an array my code is something like this (i will shortcut the array because it too long)

var bodyParser = require('body-parser');
var express = require("express");
var path = require("path");
var session = require('express-session');
var MongoClient = require('mongodb').MongoClient;
var ObjectId = require('mongodb').ObjectID;
var app = express();
var url = "mongodb://<username>:<password>@ds157342.mlab.com:57342/heroku_4p9ggb81";
MongoClient.connect(url, function(err, db) {
   if (err) throw err;
 var entery = ['غبة خاتون','حي الربيع','المغرب','القاهرة','الصليخ',.........];
   var collection = db.collection('customers');
for (var i = 0 ; i<entery.length; i++){
     var query =  {"_id" : ObjectId(),"value": entery[i],"key": i}
collection.updateOne({"_id":ObjectId("59ccdf34aabdd50011258cbf")},{ "$push": { "lookupdata": query }},{ upsert: true },
    function (err, result) {if (err) throw err;db.close();
  });
      console.log(i + "- " + entery[i] + " add data finsh ");}
app.listen(process.env.PORT || 3000, function (){
    console.log("server is started");});

my document structure like this

{
"_id" : ObjectId("59cce9fcaabdd50011258cc0"),
"formorder" : 1,
"isDisplayOnList" : true,
"issystemfield" : false,
"isMandatory" : false,
"description" : "-",
"labelname" : "name",
"displayname" : "name",
"fieldtype" : "list",
"formname" : "people",
"lookupdata" : [ 
    {
        "value" : "test",
        "key" : "1",
        "id" : "669c78c9-d086-442c-954b-d6d3f861664b"
    }
],

}

my problem that my code does not insert all the 499 entry from the array into the "lookupdata" object, it just enters 50 entry and sometimes just 120 etc, I try to change the way I write this code many times but the problem still the same.

Upvotes: 0

Views: 40

Answers (1)

Ayush Mittal
Ayush Mittal

Reputation: 559

You are inserting/updating the data without using a callback, use async module.

    `var bodyParser = require('body-parser');
    var express = require("express");
    var path = require("path");
    var session = require('express-session');
    var MongoClient = require('mongodb').MongoClient;
    var ObjectId = require('mongodb').ObjectID;
    var async = require('async');
    var app = express();
    var url = "mongodb://<username>:<password>@ds157342.mlab.com:57342/heroku_4p9ggb81";
    MongoClient.connect(url, function(err, db) {
       if (err) throw err;
     var enteries = ['غبة خاتون','حي الربيع','المغرب','القاهرة','الصليخ',.........];
       var collection = db.collection('customers');
    async.eachOf(enteries, function(entery, key, callback){
         var query =  {"value": entery,"key": key}
    collection.updateOne({"_id":ObjectId("59ccdf34aabdd50011258cbf")},{ "$push": { "lookupdata": query }},{ upsert: true },
        function (err, result) {if (err) throw err;db.close();
      });
      }, function(err){
        if(err) throw err;
      });
          console.log(i + "- " + entery[i] + " add data finsh ");}
    app.listen(process.env.PORT || 3000, function (){
        console.log("server is started");});`

Upvotes: 1

Related Questions