I am trying to load a bunch of data from a file into my mongoDB; Using node: v8.9.1, mongoose: ^4.10.8, MongoDB v3.4.1 - running on Win 10.
only a couple elements from the document are being inserted. I suspect something in the schema, but I cant be sure, the console.log reflects the date 100% correctly.
This is my data: /_mock_data/test.json
[{
"AUTHOR":"Anthony Johnson",
"TITLE":"A Little March",
"COLLECTION_TITLE":"Classic Fairytails for Children",
"FORMAT":"Trade Paperback",
"PUBLISHER":"Randon House",
"SKU":"10055",
"SUGGESTED_GRADE_LEVEL":[6],
"STATE_RATING":[{"NY":[1]}, {"PA":[1]}, {"VA":[3]}],
"AUDIO_LINK":""
},...]
Here's the code: app.js (reduced to what's relevant)
var express = require( 'express' );
var app = express();
var bodyParser = require( 'body-parser' );
var mongoose = require( 'mongoose' );
mongoose.Promise = require( 'bluebird' );
app.use( bodyParser.json() );
var Catalog = require( './models/catalog' );
mongoose.connect('mongodb://localhost:27017/test', {server: { poolSize: 5 }});
var db = mongoose.connection;
app.get( '/api/loadCatalog', function( req, res ) {
var catalog = require( './_mock_data/test.json' );
for ( var i in catalog ) {
Catalog.create( catalog[i] );
console.log( catalog[i] );
};
});
app.listen( 3000, function() {
console.log( 'server started on port 3000' );
});
/models/catalog
var mongoose = require( 'mongoose' );
// catalog schema
var catalogSchema = mongoose.Schema({
author : {
type: String
,required: false
}
,title : {
type: String
,required: false
}
,collection_title : {
type: String
,required: false
}
,format : {
type: String
,required: false
}
,publisher : {
type: String
,required: false
}
,sku : {
type: String
,required: false
}
,suggested_grade_level : {
type: Array
,required: false
}
,state_rating : {
type: [mongoose.Schema.Types.Mixed]
,required: false
}
,audio_link : {
type: String
,required: false
}
});
var Catalog = module.exports = mongoose.model( 'Catalog', catalogSchema );
// Get Items
module.exports.getItems = function( callback, limit ) {
Catalog.find( callback ).limit( limit );
};
// Add Item
module.exports.addItem = function( item, callback ) {
Catalog.create( item, callback );
};
and the errors I'm getting -
Unhandled rejection TypeError: Cannot use 'in' operator to search for '_id' in Anthony Johnson
Unhandled rejection TypeError: Cannot use 'in' operator to search for '_id' in A Little March
Unhandled rejection TypeError: Cannot use 'in' operator to search for '_id' in Classic Fairytails for Children
Unhandled rejection TypeError: Cannot use 'in' operator to search for '_id' in Trade Paperback
Unhandled rejection TypeError: Cannot use 'in' operator to search for '_id' in Randon House
Unhandled rejection TypeError: Cannot use 'in' operator to search for '_id' in 10055
Unhandled rejection TypeError: Cannot use 'in' operator to search for '_id' in 6
So the data I get looks like this
{
"_id" : ObjectId("5a28fa8503511d1d14054c92"),
"state_rating" : [ ],
"suggested_grade_level" : [ ],
"__v" : 0
}
All the string data errored and the object data went in, The "actual" data is an array of 1000's, and this is a one time import, yes I could use mongo cli to do the import, but I am only developing locally, Eventually I'll be shoving this data to an Atlas account, and as far as I can tell, this will be the way to get the data in place.
the console.log inside the loop gives me the actual data
Anthony Johnson
A Little March
Classic Fairytails for Children
Trade Paperback
Randon House
10055
[ 6 ]
[ { NY: [ 1 ] }, { PA: [ 1 ] }, { VA: [ 3 ] } ]
I'm pretty sure Im missing something basic about using the model to use the schema in this way... my "get" and "add" work fine...maybe I should do the loop in the model, not the app...
catalog[i]. I think it should include the key with it. likeCatalog.create( {your_key:catalog[i]});