我正在尝试将一堆数据从文件加载到我的mongoDB中; 使用node:v8.9.1,mongoose:^ 4.10.8,MongoDB v3.4.1 - 在Win 10上运行。
仅插入文档中的几个元素。我怀疑架构中有什么东西,但我不能确定,console.log正确地反映了100%的日期。
这是我的数据: / _ 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":""
},...]
以下是代码: app.js (缩减为相关内容)
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' );
});
/模型/目录
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 );
};
以及我得到的错误 -
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
所以我得到的数据看起来像这样
{
"_id" : ObjectId("5a28fa8503511d1d14054c92"),
"state_rating" : [ ],
"suggested_grade_level" : [ ],
"__v" : 0
}
错误的所有字符串数据和对象数据进入,“实际”数据是一个1000的数组,这是一次性导入,是的我可以使用mongo cli进行导入,但我只是在开发在本地,最终我会将这些数据推送到Atlas帐户,据我所知,这将是获取数据的方法。
循环内的console.log为我提供了实际的数据
Anthony Johnson
A Little March
Classic Fairytails for Children
Trade Paperback
Randon House
10055
[ 6 ]
[ { NY: [ 1 ] }, { PA: [ 1 ] }, { VA: [ 3 ] } ]
我很确定我错过了使用模型以这种方式使用模式的基本内容......我的“获取”和“添加”工作正常...也许我应该在模型中进行循环,而不是应用...
答案 0 :(得分:1)
你需要做一个小循环改变,
col1 col2
1: 8 cat
2: 11 cat
3: 12 cat
4: 14 cat
5: 19 cat
6: 20 cat
要将键转换为lower和camel大小写以匹配架构,您可以创建一个像这样的小辅助函数
for ( var i in catalog ) {
var key = convertStringToCamelCase(i);
Catalog.create( {key : catalog[i]} );
console.log( {key : catalog[i]} );
};
}