正确插入和/或更新许多数据集到MongoDB(使用mongoose)? [英] Correctly inserting and/or updating many datasets to MongoDB (using mongoose)?

查看:334
本文介绍了正确插入和/或更新许多数据集到MongoDB(使用mongoose)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我不时会获得POI城市数据库的新出口以及有关它们的信息,我想在我的MongoDB中使用Loopback-API获取所有数据。因此,我将数据减少到我想要的结构并尝试导入它。

So from time to time I get new exports of a cities database of POIs and info about them and I want to have all that data in my MongoDB with a Loopback-API on it. Therefore I reduce the data to my desired structure and try to import it.

我第一次收到这样的导出,我只需用insertMany()插入数据。

For the first time I receive such an export, I can simply insert the data with insertMany().

当我获得新的导出时,这意味着它包含更新的POI,我实际上希望将现有的POI替换为新的数据。所以我以为我会使用updateMany()但是我无法弄明白我是怎么做的。

When I get a new export, it means that it includes updated POIs which I actually want my existing POIs to be replaced with that new data. So I thought I'd use updateMany() but I could'nt figure out how I'd do that in my case.

这是我到目前为止所拥有的: / p>

Here's what I have so far:

const fs = require('fs');
const mongoose = require('mongoose');

const data = JSON.parse(fs.readFileSync('data.json', 'utf8'));

// Connect to database
mongoose.connect('mongodb://localhost/test', {
  useMongoClient: true
}, (err) => {
  if (err) console.log('Error', err);
});

// Define schema
let poiSchema = new mongoose.Schema({
  _id:          Number,
  name:         String,
  geo:          String,
  street:       String,
  housenumber:  String,
  phone:        String,
  website:      String,
  email:        String,
  category:     String
});

// Set model
let poi = mongoose.model('poi', poiSchema);

// Generate specified data from given export
let reducedData = data['ogr:FeatureCollection']['gml:featureMember'].reduce((endData, iteratedItem) => {
  endData = endData.length > 0 ? endData : [];

  endData.push({
    _id:          iteratedItem['service']['fieldX'],
    name:         iteratedItem['service']['fieldX'],
    geo:          iteratedItem['service']['fieldX']['fieldY']['fieldZ'],
    street:       iteratedItem['service']['fieldX'],
    housenumber:  iteratedItem['service']['fieldX'],
    phone:        iteratedItem['service']['fieldX'],
    website:      iteratedItem['service']['fieldX'],
    email:        iteratedItem['service']['fieldX'],
    category:     iteratedItem['service']['fieldX']
  });

  return endData;
}, []);

//
// HERE: ?!?!? Insert/update reduced data in MongoDB collection ?!?!?
//

mongoose.disconnect();

所以我只想更新所有已更改的内容。

So I just want to update everything that has changed.

当然如果我将它留给insertMany(),它会因双键而失败。

Of course if I leave it to insertMany() it fails due to dup key.

推荐答案

第二时间,使用mongo的更新命令, upsert 设置为 true

For the second time, use mongo's update command with upsert set to true.

db.collection.update(query, update, options)

查询中中传递 _id 更新传递对象并在选项集 upsert 中传递给 true 。这将更新文档(如果存在则创建新文档,如果该文档不存在)。

In the query pass the _id ,in update pass the object and in option set upsert to true. This will update the document if it exists creates a new document if that doesn't exist.

这篇关于正确插入和/或更新许多数据集到MongoDB(使用mongoose)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆