Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Mongodb根据大量文档的少数条件将字段从一个集合添加到另一个集合_Javascript_Mongodb_Mongoose_Nodejs Server - Fatal编程技术网

Javascript Mongodb根据大量文档的少数条件将字段从一个集合添加到另一个集合

Javascript Mongodb根据大量文档的少数条件将字段从一个集合添加到另一个集合,javascript,mongodb,mongoose,nodejs-server,Javascript,Mongodb,Mongoose,Nodejs Server,我遇到了以下情况,我需要经常更新大量集合 我有一个像下面这样的收藏 coll1 { "identification_id" : String, "name" : String, "mobile_number" : Number, "location" : String, "user_properties" : [Mixed types], "profile_url" : String } coll2 { "identification_id": String,

我遇到了以下情况,我需要经常更新大量集合

我有一个像下面这样的收藏

coll1
{
  "identification_id" : String,
  "name" : String,
  "mobile_number" : Number,
  "location" : String,
  "user_properties" : [Mixed types],
  "profile_url" : String
}

coll2
{
  "identification_id": String,
  "user_id" : String,
  "name" : String,
  "mobile_number" : Number,
  "location" : String,
  "user_properties" : String,
  "profile_url": String,
  "qualified_user" : String,
  "user_interest_stage" :Number,
  "source" : String,
  "fb_id" : String,
  "comments":String
}

updated coll1
{
  "identification_id": String,
  "name" : String,
  "mobile_number" : Number,
  "location" : String,
  "user_properties" : String,
  "profile_url": String,
  "qualified_user" : String,
  "user_interest_stage" :Number,
  "source" : String,
  "fb_id" : String,
  "comments":String
}

正如您所看到的coll1和coll2,下面将插入文档场景

  • 如果coll1中的用户基于某些场景(他可以对产品表现出兴趣)获得资格,我将在coll2中创建一个记录
  • 我可以手动从coll2中的API信息创建新记录
  • coll2中coll1的标识为用户\u id
  • 对于coll1中的一个记录,coll2中可能有多个记录
  • 现在由于某些原因,我们正在将这些集合合并为一个集合,即coll1。 我们已决定根据键“qualified_user”更新合格访问者,并更新coll1中相应的用户字段

    我已经使用NodeJS和mongoose编写了一个脚本,它将从coll1获取文档,并在coll2中验证合格的_用户,并根据以下场景进行更新

  • 如果没有合格用户,则使用默认值“不合格用户”更新文档
  • 如果有一名合格用户,则从coll2复制资格文件并在coll1中更新
  • 如果有多个合格用户,请复制第一个文档并在coll1中更新。对于coll2中的其余文档,请在coll1中创建一个新文档
  • 处理完coll1中的所有文档后,处理通过API鉴定的coll2文档,并在coll1中创建一个新文档
  • 当我运行这个脚本时,我得到了下面的错误

    <--- JS stacktrace --->
    
    ==== JS stack trace =========================================
    
    
    ==JS堆栈跟踪=========================================
    
    coll1中的文档数为1L。由于处理大量收集,我遇到了这种情况。所以我使用了skip和limit来处理所有文档,但处理所有文档花了1个小时


    有没有更好的方法来处理这些类型的数据库更新以用于大量的集合?

    您试图一次保存太多的文档,这会导致内存不足

    您有两个简单的选择:

  • 使用Mongo对结果进行迭代,而不是一次获取所有结果
  • 运行脚本时使用
    --max old space size
    标志,您可以手动设置脚本可以访问的内存量,如:
    节点--max old space size=4096 script.js
  • 尽管如此,这两种方法都不是最优的,而且假设你的规模会不断增加,这两种方法最终都不会奏效。我个人建议重新考虑数据结构。Mongo是一种非结构化语言,不能很好地处理数据复制。这意味着您“希望”将所有数据保存在一个集合中,然后在特定条件下只更新某些字段