简体   繁体   English

批量插入并忽略文档是否已存在

[英]Bulk insert and ignore if a document already exist

I have a script that scraps a website everyday at a specific hour. 我有一个脚本,可以每天在特定时间抓取一个网站。 I convert the data received into an array of objects like this: 我将接收到的数据转换成这样的对象数组:

Items = [ 
    {
      title: "some title", 
      url: "someurl.com/something" , 
      description: "some description"
     },
     {...} ,
     {...}
]

Then I use a foreach loop to insert each object of the array into MongoDB: 然后,我使用一个foreach循环将数组的每个对象插入MongoDB中:

Items.forEach( item => Collection.create(item));

Now every day when the script runs, it gets all the previous data that have been already inserted into the database and new ones. 现在,每天运行脚本时,它都会获取已插入数据库的所有先前数据和新数据。 I want to insert only new objects into the database. 我只想将新对象插入数据库。 I tried different ways but it does not work. 我尝试了不同的方法,但是没有用。

Please someone can tell me how I can get it to work? 请有人告诉我如何使它工作? Also the forEach loop I use to insert to the database I am sure there is better way to do it. 同样,我使用forEach循环将其插入数据库中,我确信有更好的方法可以做到这一点。

Use Update with upsert enabled instead of just inserting into the database. 在启用upsert使用Update ,而不仅仅是插入数据库。

var your_document = { .... };

Model.update(your_document, your_document, options, callback)
// is sent as
Model.update(query, { $set: ......}, options, callback)

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

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