如何将JSON数据放入MongoDB?

发布于 2025-01-10 17:43:44 字数 3107 浏览 0 评论 0 原文

我是 mongo 的初学者javascript,我编写了一个大概的代码,可能是将所有API JSON响应推送到我的集合数据库上,所以我应该带头使用此代码做一些看起来像某事而不是什么都不做的事情:

这个file 将 JSON api 响应的所有结果转换为卡片,但在此之前我需要存储此响应

import { defineStore } from "pinia";
import axios from "axios";
import { MongoClient } from 'mongodb'

export const useCardStore = defineStore("cardStore", {
 state: () => ({
  cards: [], 
  }),
  getters:{
     allCards: (state) => state.cards
  },
 actions: {
   async loadCards() {
   try {
    const response = await axios.get("https://api.adzuna.com/....");
    this.cards = response;
    const { data } = response;
    response.data.results.forEach((item) => {
      console.log(item);
      return item.get().then(response => {
        JSON.parse(response.body.text());
        var MongoClient = require('mongodb').MongoClient;
        var url = 'mongodb://localhost:27017/MyCollection';

        MongoClient.connect(url, function(db) {
        
        var myDB = db.db("JobSeeker");
        
        var myobj = [item];
        
          myDB.collection("cardsJobs").insertMany(myobj, function(res) {
          console.log("Number of item inserted: " + res.insertedCount);
          db.close();
        });
      })
     });
    });
  } 
  catch (error) {
    console.log(error);
      }
  },
 },
});

Im beginner on mongo & javascript, i made a approximate code may be to push all API JSON response on my collection db, so i should take the lead with this code to do something that looks like something rather than doing nothing :

The goal of this file is convert to a card all result of JSON api response, but before that i need to store this response

import { defineStore } from "pinia";
import axios from "axios";
import { MongoClient } from 'mongodb'

export const useCardStore = defineStore("cardStore", {
 state: () => ({
  cards: [], 
  }),
  getters:{
     allCards: (state) => state.cards
  },
 actions: {
   async loadCards() {
   try {
    const response = await axios.get("https://api.adzuna.com/....");
    this.cards = response;
    const { data } = response;
    response.data.results.forEach((item) => {
      console.log(item);
      return item.get().then(response => {
        JSON.parse(response.body.text());
        var MongoClient = require('mongodb').MongoClient;
        var url = 'mongodb://localhost:27017/MyCollection';

        MongoClient.connect(url, function(db) {
        
        var myDB = db.db("JobSeeker");
        
        var myobj = [item];
        
          myDB.collection("cardsJobs").insertMany(myobj, function(res) {
          console.log("Number of item inserted: " + res.insertedCount);
          db.close();
        });
      })
     });
    });
  } 
  catch (error) {
    console.log(error);
      }
  },
 },
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗳卜坏 2025-01-17 17:43:44
//modify your code like this
var url = "API CALL HERE"

request(url, function(error, response, body){

 if(!error && response.statusCode == 200){
    var data = JSON.parse(body);
    res.send(data);

    var MongoClient = require('mongodb').MongoClient;
    var url = "mongodb://localhost:27017/mydb";

    MongoClient.connect(url, function(err, db) {
      if (err) throw err;
      var myobj = [];
      myobj.push(data);
      db.collection("dabas").insertMany(myobj, function(err, res) {
        if (err) throw err;
        console.log("Number of documents inserted: " + res.insertedCount);
        db.close();
      });
    });
 }
});

//your problem is you passing obj not array
//see below link for your reference

https://docs.mongodb.com/manual/reference/method /db.collection.insertMany/

//modify your code like this
var url = "API CALL HERE"

request(url, function(error, response, body){

 if(!error && response.statusCode == 200){
    var data = JSON.parse(body);
    res.send(data);

    var MongoClient = require('mongodb').MongoClient;
    var url = "mongodb://localhost:27017/mydb";

    MongoClient.connect(url, function(err, db) {
      if (err) throw err;
      var myobj = [];
      myobj.push(data);
      db.collection("dabas").insertMany(myobj, function(err, res) {
        if (err) throw err;
        console.log("Number of documents inserted: " + res.insertedCount);
        db.close();
      });
    });
 }
});

//your problem is you passing obj not array
//see below link for your reference

https://docs.mongodb.com/manual/reference/method/db.collection.insertMany/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文