简体   繁体   English

Mongo数组限制

[英]Mongo array limit

I'm trying to insert an object in Mongo using the native node library. 我正在尝试使用本机节点库在Mongo中插入对象。 I have an object that has an array property that has 1620 objects. 我有一个具有1620个对象的array属性的对象。 In the documentation it says mongo has support for nesting up to 100 levels which I'm not running into. 在文档中,它说mongo支持嵌套多达100个我没有遇到过的级别。 The objects in the array are just objects, they don't have any arrays. 数组中的对象只是对象,没有任何数组。 The size of the total document with all the items is only 456Kb. 所有项目的总文档大小仅为456Kb。 If I limit the array to 788 objects they do get saved in the database but it crashses the app. 如果我将数组限制为788个对象,它们的确会保存在数据库中,但会使应用程序崩溃。 Am I running into a problem with the native mongo library for node? 我是否遇到节点的本机mongo库问题? Missing a setting for database? 缺少数据库设置?

var droplist = convertOptionList(data);

MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) {
  if(err) {console.log('error'); throw err;}

  var collection = db.collection('droplist');

  collection.insert(droplist, function(err, objects) {
    if(err) {
      console.log('error');
      throw err;
    }
    //never see this message. app crashes but with fewer items the query passes
    console.log("A-Ok");
  });
  db.close();
});

Update 更新资料

Working version of the code. 代码的工作版本。 Slight change to move the db.close() inside of the callback for insert and preventing the db.close to be called before the objects have all been inserted. 稍作更改即可将db.close()移动到用于插入的回调中,并防止在对象全部插入之前调用db.close。

var droplist = convertOptionList(data);

MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) {
  if(err) {console.log('error'); throw err;}

  var collection = db.collection('droplist');

  collection.insert(droplist, function(err, objects) {
    if(err) {
      console.log('error');
      throw err;
    }
    db.close();
  });
});

Rookie mistake. 新秀错误。 I put my db.close() outside of my callback so the connection was closing before the query could run/finish. 我将db.close()放在回调之外,因此在查询可以运行/完成之前,连接已关闭。 Moved my db.close() into the callback and everything works now. 将我的db.close()移到回调中,现在一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM