MongoDB播种嵌套的对象数组

时间:2018-05-01 21:35:02

标签: javascript mongodb api mongoose mongoose-schema

我有一个MongoDB,我从JSON文件中播放数据库的数据。 JSON文件结构正确,种子文件大部分都在工作。我遇到的问题是嵌套的对象数组只有Mongo ID,而不是整个对象。我已经尝试了许多集合函数(collection.insert(), collection.save(), ...),但它们都返回相同的东西。

以下是数据的外观,其中项目是嵌套的对象数组:

{
    "client_name": "2017 Colorado Hospital Association",
    "projects": [
      {
        "project_name": "2017 Project",
        "p_number": "CHA201652",
        "hours_used": 356.03,
        "approved_hours": null
      }
    ]
  }

以下是返回的内容:

{
  "projects": [
    "5ae8d05b5ee3f79bfdefd357"
  ],
  "_id": "5ae8d05b5ee3f79bfdefd39d",
  "client_name": "2017 Colorado Hospital Association",
  "__v": 0
},

所以这是我的seed.js文件:

// Require DB Models
const db = require('./models');
const clientList = require('./data/client.json');
const userList = require('./data/user.json');
const projectList = require('./data/projects.json');

// SEEDING THE DB
db.User.remove({}, (err, users) => {
  console.log('Removed All Users');
  db.User.create(userList, (err, users) => {
    if(err) {
      console.log('DB User creation err: ', err);
    }
    console.log('Created User DB!');
  });
});

db.Project.remove({}, (err, projects) => {
  console.log('Removed Projects');
  db.Project.create(projectList, (err, projects) => {
    if(err) {
      console.log('DB Project Creation Err: ', err);
    }
    console.log('Created Project DB!');

    // Client Creation
    db.Client.remove({}, (err, clients) => {
      console.log('Removed All Clients');
      clientList.forEach((clientData) => {
        let client = new db.Client({
          client_name: clientData.client_name,
          projects: []
        });
        // console.log('client data: ', clientData);
        clientData.projects.forEach((projectData) => {
          // console.log('project data: ', projectData);
          db.Project.findOne({p_number: projectData.p_number}, (err, foundProject) => {
            if(err) {
              console.log('Find Project Err: ', err);
            }
            // console.log('found project: ', foundProject);
            client.projects.push(foundProject);
            // console.log('client post push: ', client);
            client.save((err, savedClient) => {
              if(err) {
                console.log('Save Client Err: ', err);
              }
            });
          });
        });
      });
    });
  });
});

Project.js文件:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const ProjectSchema = new Schema({
    project_name: String,
    p_number: String,
    hours_used: Number,
    approved_hours: Number,
    remaining_hours: Number
});

const Project = mongoose.model('Project', ProjectSchema);

module.exports = Project;

Client.js文件:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const Project = require('./project');

const ClientSchema = new Schema({
  client_name: String,
  projects: [
    {type: Schema.Types.ObjectId, ref: 'Project '}
  ]
});

// MODEL
const Client = mongoose.model('Client', ClientSchema);

// EXPORT
module.exports = Client;

我的index.js文件:

const mongoose = require('mongoose');
mongoose.connect(process.env.MONGODB_URI || process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || 'mongodb://localhost/client-portal');

module.exports.Client = require('./client');
module.exports.User = require('./user');
module.exports.Project = require('./project');

我查看过很多关于此问题的堆栈问题,并且$set没有用,因为它必须通过p_number找到所有项目,然后将它关联到客户端。

任何帮助都会非常感谢!

0 个答案:

没有答案