【发布时间】:2018-05-22 13:35:08
【问题描述】:
我正在尝试将文件中的一堆数据加载到我的 mongoDB 中; 使用节点:v8.9.1,猫鼬:^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' );
});
/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 );
};
以及我遇到的错误 -
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 ] } ]
我很确定我错过了一些关于使用模型以这种方式使用模式的基本知识......我的“get”和“add”工作正常......也许我应该在模型中做循环,而不是应用程序...
【问题讨论】:
-
你正在传递
catalog[i]。我认为它应该包括钥匙。喜欢Catalog.create( {your_key:catalog[i]}); -
是的,Kiran,谢谢 - 我看到我发布它的方式不清楚 - 对象周围应该有一个数组,因为文件是一个数组。我已经解决了。
标签: node.js mongodb express mongoose