通过Mongoose将JSON数据发送到MongoDB

时间:2015-03-12 16:09:05

标签: javascript mongodb mongoose

我目前有一个JSON对象,我无法通过mongoose将其持久保存到mongoDB数据库。我有两个主要问题。

  1. 我的模型是否正确设置了下面的JSON对象?我之前从未使用过子文档,所以这是第一个。
  2. 当我尝试保留数据时,我一直收到错误(错误:"对象不是函数")。我究竟做错了什么?
  3. 所有代码均在下方。

    以下是我设置mongoose的方法。

    var mongoose = require('mongoose');
    mongoose.connect('mongodb://localhost/yjs');
    var db = mongoose.connection;
    db.on('error', console.error.bind(console, 'mongodb connection error:'));
    
    var Schema = mongoose.Schema;
    
    var resultsSchema = new Schema({
        jobtitle: String,
        company: String,
        city: String,
        state: String,
        country: String,
        formattedLocation: String,
        source: String,
        date: String,
        snippet: String,
        url: String,
        onmousedown: String,
        jobkey: String,
        sponsored: Boolean,
        expired: Boolean,
        indeedApply: Boolean,
        formattedLocationFull: String,
        formattedRelativeTime: String
    });
    
    var jobSchema = new Schema({
        version: Number,
        query: String,
        location: String,
        dupefilter: Boolean,
        highlight: Boolean,
        radius: Number,
        start: Number,
        end: Number,
        totalResults: Number,
        pageNumber: Number,
        results: [resultsSchema]
    
    });
    
    
    var Jobs = mongoose.model('Jobs', jobSchema);
    
    module.exports = {
        Jobs: Jobs
    }
    

    这是我的JSON对象:

    {
      version: 2,
      query: "javascript $19,900-$20,900",
      location: "New York, NY",
      dupefilter: true,
      highlight: true,
      radius: 25,
      start: 1,
      end: 3,
      totalResults: 3,
      pageNumber: 0,
      results: [
        {
          jobtitle: "Web Application Developer",
          company: "Sogeti",
          city: "New York",
          state: "NY",
          country: "US",
          formattedLocation: "New York, NY",
          source: "Sogeti",
          date: "Sat, 15 Nov 2014 08:50:24 GMT",
          snippet: "Roles/ Responsibility/ Qualifications: Position Location: Raritan, New Jersey Sogeti is a leading provider of technology and software testing, specializing...",
          url: "http://www.indeed.com/viewjob?jk=5c3af4fc30704f52&qd=PgQusJx_gGiiAmACeKxgepvuLa6C1I07uk43fDZ3DSq8GvDGPvLAOvUmXryxs9bezoA6A_gJOaAYR60u2fV5hFgosDLOUjUrxWWMfxXcFW0txtBb9O5egndMhGuvOU46RMGL25yEMtQuUuTH37UmVA&indpubnum=9222093808317380&atk=19g75527l0np13ao",
          onmousedown: "indeed_clk(this, '2276');",
          jobkey: "5c3af4fc30704f52",
          sponsored: false,
          expired: false,
          indeedApply: false,
          formattedLocationFull: "New York, NY",
          formattedRelativeTime: "30+ days ago"
        },
        {
          jobtitle: "Interactive Designer – New York",
          company: "Unruly Ltd",
          city: "New York",
          state: "NY",
          country: "US",
          formattedLocation: "New York, NY",
          source: "Unruly Ltd",
          date: "Fri, 28 Nov 2014 00:05:17 GMT",
          snippet: "WANTED: Positioned at the intersect of the three fastest-growing trends in digital advertising – programmatic, video and mobile – Unruly is a high-growth tech",
          url: "http://www.indeed.com/viewjob?jk=98f978c3e33061d1&qd=PgQusJx_gGiiAmACeKxgepvuLa6C1I07uk43fDZ3DSq8GvDGPvLAOvUmXryxs9bezoA6A_gJOaAYR60u2fV5hFgosDLOUjUrxWWMfxXcFW0txtBb9O5egndMhGuvOU46RMGL25yEMtQuUuTH37UmVA&indpubnum=9222093808317380&atk=19g75527l0np13ao",
          onmousedown: "indeed_clk(this, '2276');",
          jobkey: "98f978c3e33061d1",
          sponsored: false,
          expired: false,
          indeedApply: true,
          formattedLocationFull: "New York, NY",
          formattedRelativeTime: "30+ days ago"
        },
        {
          jobtitle: "Java Angular JS Developer",
          company: "UST Global",
          city: "Edison",
          state: "NJ",
          country: "US",
          formattedLocation: "Edison, NJ",
          source: "Dice",
          date: "Wed, 18 Feb 2015 14:06:16 GMT",
          snippet: "Rakesh.Sasidharanust-global.com <b>Javascript</b>, Angular JS, JQuery, Ext. Java Angular JS Developer 5 years experience in Java and web development Should have...",
          url: "http://www.indeed.com/viewjob?jk=f347ca5dc7439231&qd=PgQusJx_gGiiAmACeKxgepvuLa6C1I07uk43fDZ3DSq8GvDGPvLAOvUmXryxs9bezoA6A_gJOaAYR60u2fV5hFgosDLOUjUrxWWMfxXcFW0txtBb9O5egndMhGuvOU46RMGL25yEMtQuUuTH37UmVA&indpubnum=9222093808317380&atk=19g75527l0np13ao",
          onmousedown: "indeed_clk(this, '2276');",
          jobkey: "f347ca5dc7439231",
          sponsored: false,
          expired: false,
          indeedApply: false,
          formattedLocationFull: "Edison, NJ 08817",
          formattedRelativeTime: "22 days ago"
        }
      ]
    }
    

    我是如何尝试保留数据的:

    var job = new Jobs(json_object);
    
    Jobs.create(json_object, function (err, job) {
        if (err) return handleError(err);
    
        return job();
    });
    

    任何指导都将不胜感激!抱歉JSON对象的格式化,我无法在更改格式时将其保留为代码。它基本上是一个对象,其中一个键值是一个对象数组。

1 个答案:

答案 0 :(得分:1)

  models.Jobs.create(json_object, function (err) {
        if (err) {return handleError(err);

        } else {
            console.log('data collected')
        }

    })

上面的代码解决了这个问题。也似乎子文档正常工作。我完全看了一下我要求的模块。