如何将来自API URI的JSON文件直接保存到NodeJS中的mongoDB集合中

huangapple go评论68阅读模式
英文:

How to save an JSON file from an API URI directly to a mongoDB collection in NodeJS

问题

I'll provide a translation of the code portion you provided:

**1. 创建axios客户端**

    const api = axios.create({
      method: 'GET',
      baseURL: 'https://api.someAPI.com',
      headers: {
        'API_KEY': process.env.API_KEY,
        Accept: 'application/json',
      },
    });

**2. 获取JSON并保存到MongoDB的新集合中**

    function getAPI_AndSaveToDB() {
      api('/api/v3/exactURI')
        .then((value) => res.json(value.data))
        .then(dbo.createCollection("myCollection")
        .then(myCollection.insertMany(value));
    }

I've translated the code parts as requested. If you need any further assistance, feel free to ask.

英文:

I'm pretty new to MERN and I wonder if it possible at all. If you have any experience please help 如何将来自API URI的JSON文件直接保存到NodeJS中的mongoDB集合中

Here my naive code:

1. Create axios client

const api = axios.create({
  method: 'GET',
  baseURL: 'https://api.someAPI.com',
  headers: {
    'API_KEY': process.env.API_KEY,
    Accept: 'application/json',
  },
});

2. Get JSON and save it to a new collection on MongoDB

function getAPI_AndSaveToDB() {
  api('/api/v3/exactURI')
    .then((value) => res.json(value.data))
    .then(dbo.createCollection("myCollection")
    .then(myCollection.insertMany(value));
}

答案1

得分: 1

单个文档

这是一个使用 Axios 进行请求和官方 MongoDB 驱动程序的示例。您可以从中学习,并根据您的需求进行调整:也许您想考虑使用像 Mongoose 这样的 ORM,这取决于您的项目。

const axios = require('axios');
const { MongoClient } = require('mongodb');

async function fetchDataAndSaveToMongoDB() {
  // 从URL获取JSON数据
  const { data } = await axios.get('https://example.com/data.json');

  // 连接到 MongoDB
  const client = new MongoClient('mongodb://localhost:27017');
  await client.connect();

  try {
    // 选择数据库和集合
    const db = client.db('mydb');
    const collection = db.collection('mycollection');

    // 将JSON数据插入集合
    const result = await collection.insertOne(data);
    console.log(`${result.insertedCount} 个文档已插入`);
  } finally {
    // 关闭MongoDB连接
    await client.close();
  }
}

fetchDataAndSaveToMongoDB();

请注意,要使此代码正常工作,您需要在您的 Node.js 项目中安装 axios 和 mongodb 模块。您可以通过在项目目录中运行以下命令来执行此操作:

$ npm install axios mongodb

多个文档

如果请求的文件是一个数组,您可以将insertOne替换为MongoDB驱动程序的insertMany方法,以将数组的每个元素作为集合中的单独文档插入。

英文:

Single document

This is an example using Axios for the request, and the official MongoDB driver. You can learn from it and adapt to your needs: maybe you want to consider using an ORM like Mongoose as well, depending on your project.

const axios = require('axios');
const { MongoClient } = require('mongodb');

async function fetchDataAndSaveToMongoDB() {
  // Fetch the JSON data from a URL
  const { data } = await axios.get('https://example.com/data.json');

  // Connect to MongoDB
  const client = new MongoClient('mongodb://localhost:27017');
  await client.connect();

  try {
    // Select the database and collection
    const db = client.db('mydb');
    const collection = db.collection('mycollection');

    // Insert the JSON data into the collection
    const result = await collection.insertOne(data);
    console.log(`${result.insertedCount} document(s) inserted`);
  } finally {
    // Close the MongoDB connection
    await client.close();
  }
}

fetchDataAndSaveToMongoDB();

Note that you will need to install the axios and mongodb modules in your Node.js project for this code to work. You can do so by running the following command in your project directory:

$ npm install axios mongodb

Multiple documents

If the requested file is an array, you can replace the insertOne with insertMany method of the MongoDB driver to insert each element of the array as a separate document in the collection.

答案2

得分: 0

Certainly, I'll provide the translated code without any additional content:

const axios = require('axios');
const { MongoClient } = require('mongodb');

async function fetchAndSaveDataToDB() {
  // 从 API URL 获取 JSON 数据
  const api = axios.create({
    method: 'GET',
    baseURL: process.env.BASE_URL,
    headers: {
      API_KEY: process.env.API_KEY,
      Accept: 'application/json',
    },
  });
  const { data } = await api.get(process.env.URI);

  // 将 JSON 数据插入集合
  await mongoClient.connect();
  try {
    const database = mongoClient.db('myDB');
    // 删除旧集合
    const oldCollection = 'collection_12';
    const isDropped = database.collection(oldCollection).drop();
    if (!isDropped) {
      console.log(`错误:: 未删除 ${oldCollection}`);
    } else {
      console.log(`${oldCollection} 成功删除`);
    }

    // 将 JSON 数据插入新集合
    const collectionName = 'collection_14';
    const theNewCollection = database.collection(collectionName);
    const options = { ordered: true }; // 防止插入文档失败
    const result = await theNewCollection.insertMany(data, options);
    console.log(
      `${result.insertedCount} 个文档已成功插入 ${collectionName}`
    );
  } finally {
    await mongoClient.close();
  }
}

fetchAndSaveDataToDB().catch(console.dir);
英文:

UPDATE For the moment I follow the instuctions from @Ezequias Dinella (Thank you very much!) and have this code witch is not ready but a step forward.

const axios = require('axios');
const { MongoClient } = require('mongodb');

async function fetchAndSaveDataToDB() {
  // Fetch the JSON data from a API URL
  const api = axios.create({
    method: 'GET',
    baseURL: process.env.BASE_URL,
    headers: {
      API_KEY: process.env.API_KEY,
      Accept: 'application/json',
    },
  });
  const { data } = await api.get(process.env.URI);

  // Insert the JSON data into the collection
  await mongoClient.connect();
  try {
    const database = mongoClient.db('myDB');
    //drop old collection
    const oldCollection = 'collection_12';
    const isDropped = database.collection(oldCollection).drop();
    if (!isDropped) {
      console.log(`ERROR:: ${oldCollection} not dropped`);
    } else {
      console.log(`${oldCollection} successfully dropped`);
    }

    // Insert the JSON data into a new collection
    const collectionName = 'collection_14';
    const theNewCollection = database.collection(collectionName);
    const options = { ordered: true }; //prevents insert docs if one fails
    const result = await theNewCollection.insertMany(data, options);
    console.log(
      `${result.insertedCount} documents in ${collectionName} successfully inserted`
    );
  } finally {
    await mongoClient.close();
  }
}

fetchAndSaveDataToDB().catch(console.dir);

huangapple
  • 本文由 发表于 2023年4月11日 05:28:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定