java查询MongoDB整理(待续)

 

1.select * from user where name = "tom"

  

DBCollection dbCollection = getDb().getCollection(“user”);//数据表
        DBObject first = new BasicDBObject();
        first.put("name", "tom");

       // 确定查询对象唯一,查询结果为object
        DBObject findOne = dbCollection.findOne(first);

       // 结果集搜索,查询结果为集合
        DBCursor dbCollection1  = dbCollection.find(first);

 

2. select * from user where name = "tom" and age !=28

DBCollection dbCollection = getDb().getCollection(“user”);//数据表
        DBObject basicDBObject = new BasicDBObject();
        basicDBObject.put("name", "tom");
        basicDBObject.put("age",new BasicDBObject("$ne", 28));
        DBCursor cursor = dbCollection.find(basicDBObject);

 

3.select count(distinct name ) from user where age =23

DBCollection dbCollection = getDb().getCollection(“user”);//数据表
        int count = dbCollection.distinct("name", new BasicDBObject("age", 23)).size();

 

4.select * from user where name = "tom" order by createTime

DBCollection dbCollection = getDb().getCollection(collectionName);

            DBObject queryObj= new BasicDBObject();

           queryObj.put("name","tom");
            DBObject sortQuery = new BasicDBObject();//根据创建时间段倒序排列
            sortQuery.put("createTime", -1);
            cursor = dbCollection.find(queryObj).sort(sortQuery);

 

5.select * from user where ((name ="tom" and age =23) or (name="tom" and age =28) )and createTime > "2019-09-01" order by createTime dese

 

DBCollection dbCollection = getDb().getCollection("user");
           
            DBObject queryObj = new BasicDBObject();
            queryObj.put("name", "tom");
            queryObj.put("age", 23);
 
            DBObject basicDBObject = new BasicDBObject();
            basicDBObject.put("name", "tom");
            basicDBObject.put("age", 28);
    
            BasicDBList basicDBList = new BasicDBList();
            basicDBList.add(queryObj);
            basicDBList.add(basicDBObject);
            DBObject basicDBObject2 = new BasicDBObject();
            basicDBObject2.put("$or", basicDBList);
            basicDBObject2.put("createTime", new BasicDBObject("$gte","2019-09-01"));
     
            cursor = dbCollection.find(basicDBObject2).sort(new BasicDBObject("createTime",-1));
            

 

6. select * from user where age not in (23,24) and name = "tom"

DBCollection dbCollection = getDb().getCollection(dbMap.get("user"));

            DBObject queryObj = new BasicDBObject();
            queryObj.put("name", "tom");
            BasicDBList values = new BasicDBList();
            values.add(23);
            values.add(24);
            queryObj.put("age", new BasicDBObject("$nin",values));
            cursor = dbCollection.find(queryObj);

 

7. sum  count 聚合

DBCollection dbCollection = getDb().getCollection(collectionName);//消息表
        BasicDBObject where = new BasicDBObject();
        where.put("时间范围", "三个月");
        where.put("from", wxh);
        where.put("to", hyzh);
        DBObject match = new BasicDBObject("$match", where);//match该微信号的所有好友微信号
        DBObject regroupFields = new BasicDBObject("_id", "$wxh");// 根据哪个字段
        if( 1 == type){
            regroupFields.put("num" ,new BasicDBObject("$sum", "$ltju"));//聊天次数sum
        }else if(2==type){     
            regroupFields.put("num" , new BasicDBObject("$count", "$ltts"));//聊天天数count
        }
        
        DBObject regroup = new BasicDBObject("$group", regroupFields);//$group
        List<DBObject> ops = new ArrayList<>();
        ops.add(match);
        ops.add(regroup);
        this.outputOne = dbCollection.aggregate(ops);
        AggregationOutput output = dbCollection.aggregate(ops);
        Iterator<DBObject> iterator = output.results().iterator();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值