我遇到了一个问题,即如何正确地将格式ID添加到格式化的Json数据中以存储在我的集合中。
这是我用来存储所述数据的模型和代码:
模型设置
var mongoose = require('mongoose')
, Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;
var user_schema = new Schema({
_id: ObjectId,
created_at: { type: Date, default: Date.now },
user_id: Number,
name: String,
user_name: String,
tweet_count: Number,
friends_count: Number,
followers_count: Number,
time_zone: String,
last_tweet:{}
});
var User = mongoose.model('Users', user_schema);
收集设置
var Schema = mongoose.Schema,
ObjectID = Schema.ObjectId;
var newUser = new User({
_id: ObjectID,
created_at: user_rest_data.status.created_at,
user_id: user_rest_data.id,
name: user_rest_data.name,
user_name: user_rest_data.screen_name,
tweet_count: user_rest_data.statuses_count,
friends_count: user_rest_data.friends_count,
followers_count:user_rest_data.followers_count,
time_zone: user_rest_data.time_zone,
last_tweet: user_rest_data.status});
newUser.save(function(err){
if(err) console.log(util.inspect(err));
else
console.log("New document has been added!");
});
我收到的输出始终是:
{ [ValidationError: Users validation failed]
message: 'Users validation failed',
name: 'ValidationError',
errors:
{ _id:
{ [CastError: Cast to ObjectID failed for value "function ObjectId(key, options) {
SchemaType.call(this, key, options, 'ObjectID');
}" at path "_id"]
message: 'Cast to ObjectID failed for value "function ObjectId(key, options) {\n SchemaType.call(this, key, options, \'ObjectID\');\n}" at path "_id"',
name: 'CastError',
kind: 'ObjectID',
value: [Object],
path: '_id' } } }