zoukankan      html  css  js  c++  java
  • Mongoose如何实现统计查询、关联查询

    [问题]Mongoose如何实现统计查询、关联查询
     发布于 4 年前  作者 a272121742  13025 次浏览

    最近业务上提出一个需求,要求能做统计,我们设计的文档集,统计可能跨越的文档会比较多,想问一下,mongoose能实现统计查询和关联查询吗? 例如student文档对象有subject文档对象,subject文档对象有score属,查询所有学员的成绩。我个人的思路就是一般的解决方案,先查询所有学员,再查询所有分数,然后遍历做累加统计

    StudentDao.find().select('_id').exec(function(err,stus){
        SubjectDao.find().where('student_id').in(stus).exec(function(err,subs){
            var sum = 0;
            for(var i = 0; i < subs.length; i++){
                sum += subs[i].score;
            }
            console.log(sum);
        });
    });

    心想只要提供了关联查询和统计查询,这样的代码会变得简化,不知道熟悉mongoose或者做过mongodb开发各位有没有遇见过,真心求解,谢谢!

    6 回复

    我遇到过。。统计查询的。后来解决办法是,扫库之后重新加了个总数的字段,每次更新都对字段进行+=操作。需要总数就取总数了。。速度还最快。。

    小应用用mapreduce吧。不过效率挺低的了。还是做增量好些。。无论啥数据库- -。

     

    ######mongoose api手册的统计的方法:

    Kitten.where('color', 'black').count(function (err, count) {
      if (err) return handleError(err);
      console.log('there are %d black kittens', count);
    })

    地址:http://mongoosejs.com/docs/api.html#query_Query-count mongodb作是文件型数据库,作关联并没有什么优势.我列出其中一些方法你可以参考一下:

    ##一:通过mongodb的mapreduce进行查询 优点是没有数据冗余或数据不一致问题,并且不需多次查询。缺点是mongodb的mapreduce基于javascript引擎(目前是spider monkey),单线程运行,所以效率略差,不适合实时查询 ##二:通过良好的设计数据模型来做到

    var postSchema = new Schema({
      author : {type : String}, 
      title : {type : String, require : true},
      content : {type : String, require : true},
      comment : {
        owner : {type : String},
        subject : {type: String, require},
        content : {type String, require}
      }
    });
    myPostModel.find({ 'comment.subject' : /car*/ }).exec(function(err, result){
        Do some stuff with the result...
    });
     

    对了,补充一下,还有用ref做关联,例如:

    var mongoose = require('mongoose')
      , Schema = mongoose.Schema
      
    var PersonSchema = new Schema({
      name    : String,
      age     : Number,
      stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }]
    });
    
    var StorySchema = new Schema({
      _creator : { type: Schema.Types.ObjectId, ref: 'Person' },
      title    : String,
      fans     : [{ type: Schema.Types.ObjectId, ref: 'Person' }]
    });
    
    var Story  = mongoose.model('Story', StorySchema);
    var Person = mongoose.model('Person', PersonSchema);
    #关联查询
    Story
    .findOne({ title: /timex/ })
    .populate('_creator')
    .exec(function (err, story) {
      if (err) return handleError(err);
      console.log('The creator is %s', story._creator.name); // prints "The creator is Aaron"
    })
     

    恩,populate是个好方法,尝试多次之后发现他比较严格,需要匹配完全疲惫model_name和schema_filed。中间用个忽略大小写就可以实现了

     

    想问一下,三联查询怎么做? 比如A-ref-B,B-ref-C,在查A的时候把ABC都查出来

    A:{
        B:{
            C:{
            }
        }
    }
     

    @a272121742 其实你看到StorySchema model就是作了两个关联,只不过是放在同一级,向你这个相互关联的话确实不适合用这种方法,只能通过第二种方法设计数据模型来做了,如果你强制先用mapreduce在用populate的话太耗性能了,你这么多关系数据,建议你mysql+mongodb同时使用,关系型数据用mysql包 npm install mysql require ‘mysql’,如果是要牺牲性能作为代价的话用mongodb就没有意义了

  • 相关阅读:
    第 13 章 外观模式
    第 12 章 桥接模式
    第 10 章 适配器模式
    第 7 章 原型模式
    PHP的预处理语句的使用
    PHP的异常处理
    PHP中PDO函数的使用
    PHP的PDO模式数据操作
    PHP的 Final关键字、类(文件)的加载和命名空间
    PHP的魔术常量和魔术方法
  • 原文地址:https://www.cnblogs.com/laiqun/p/5669196.html
Copyright © 2011-2022 走看看