angular+node数据可视化应用,mongoose如何写求助呀!
发布于 9个月前 作者 starinecust 681 次浏览

尝试的各种方法,看了好多资料,可是mongoose的资料除了那个基础篇,其他的真是少的可怜呀,求大神帮助,给我个建议- -! 数据是这样的

{
 "d10000000005": {
  "DISMISS": {
   "NONE": {
    "all": 2293, 
    "user": 1583
   }
  }, 
  "CLICK": {
   "NONE": {
    "all": 92, 
    "user": 92
   }
  }, 
  "SHOW": {
   "NONE": {
    "all": 2682, 
    "user": 1683
   }
  }
 }, 
 "d10000000007": {
  "DISMISS": {
   "NONE": {
    "all": 7788, 
    "user": 7479
   }
  }, 
  "CLICK": {
   "NONE": {
    "all": 51, 
    "user": 47
   }
  }, 
  "SHOW": {
   "NONE": {
    "all": 11656, 
    "user": 9543
   }
  }
 }
}``` 

这是一天的数据,里面可能还有08,09等等不一定有多少,我想按日期存在mongo里面,但是这个Schema应该怎么写呢,想不出一个合理的结构。尝试了好半天,最后灵机一动写成这样,我以为能行...结果是我想多了...难道是我mongoose写错了?有木有大神能给我点建议呢,
```js\n
var mongodb = require('./mongodb');
var Schema = mongodb.mongoose.Schema;
var imeSchema = new Schema({
    date:String,
    data:Schema.Types.Mixed
}
var IMEModel = mongodb.mongoose.model('IME',imeSchema);
var dbFunc = function(){};

dbFunc.prototype.save = function(obj, callback) {
    var instance = new IMEModel(obj);
    instance.save(function(err){
        callback(err);
    });
};

dbFunc.prototype.findByIdAndUpdate = function(obj,callback){
    var _id=obj._id;
    delete obj._id;
    IMEModel.findOneAndUpdate(_id, obj, function(err,obj){
        callback(err, obj);
    });
};


dbFunc.prototype.findByName = function(name, callback) {
    IMEModel.findOne({name:name}, function(err, obj){
        callback(err, obj);
    });
};

module.exports = new dbFunc();

mongoose调用的写法也是参考了好多资料,也不知道写的对不对也贴出来吧…

//mongodb.js
var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/neeyaChart');
//两个连接数据库的方式有什么区别呢?
//var db = mongoose.createConnection('localhost','neeyaChart');
//db.on('error',console.error.bind(console,'连接错误:'));
//db.once('open',function(){
//    console.log('连接成功');
//});
exports.mongoose = mongoose;

下面是我模拟的一段数据,读进来存入数据库,跪求大神指教呀

var ime = require('./models/IMEdata');
fs.readFile('app/data/20140424',{'encoding':'utf-8'},function(err,data){
    if(err){return console.log(err) ;}
    var dataObj= eval("(" + data + ")");
        ime.save({"20140424":dataObj},function(err){
            console.log('err:'+err);
        });
});```
2 回复

看一些老外的书籍吧。《Mongoose for Application Development》或许对你有些帮助。:)

稍微转换一下把dxxxxx这样的key做成value就可以了:

var imeSchema = new Schema({
    _id: String, // 'dxxxxx'
    date: Date,
    DISMISS: {
        NONE: {
            all: Number,
            user: Number
        }
    },
    CLICK: {
        NONE: {
            all: Number,
            user: Number
        }
    },
    SHOW: {
        NONE: {
            all: Number,
            user: Number
        }
    }
});

var IMEModel = mongoose.model('IME', imeSchema);

fs.readFile('app/data/20140424', {encoding: 'utf-8'}, function(err, data) {
    if (err) return console.error(err);
    
    var date = new Date(2014,4,24);
    _.forIn(eval(data), function(value, key) {
        var ime = new IMEModel(_.extend({_id: key, date: date}, value));
        ime.save(console.error);
    });
});

mongoose具体使用只能看文档了,这没什么捷径可走的

回到顶部