Mongo 积累

根据字符串(多个)模糊查询

DB filterDb = DBUtil.getDB(Constants.MONGO_DB_DOCINFO);
        DBCollection collection = filterDb
                .getCollection(Constants.MONGO_COLL_DOCINFO);
        List result = new ArrayList();
BasicDBObject queryObject = new BasicDBObject();
        boolean flag = false;// st和et至少传一个,才会拼装时间过滤条件
        BasicDBObject basicDBObject = new BasicDBObject();
        if (null != channel && channel != 0) {
            queryObject.append("ch", channel);
        }
        try {
            if (!st.equals("") && !st.equals("null")) {
                if (st.length() == 10) {
                    st = st + " 00:00:00";
                }
                Date stTime = DateUtil.string2UtilDate(st,
                        "yyyy-MM-dd HH:mm:ss");
                basicDBObject.append("$gte", stTime.getTime());
                flag = true;
            }
            if (!et.equals("") && !et.equals("null")) {
                if (et.length() == 10) {
                    et = et + " 23:59:59";
                }
                Date etTime = DateUtil.string2UtilDate(et,
                        "yyyy-MM-dd HH:mm:ss");
                basicDBObject.append("$lte", etTime.getTime());
                flag = true;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (flag) {
            queryObject.append("pt", basicDBObject);
        }
        //模糊查询 
        if (null != andWord && andWord.length() > 0) {
            Pattern pattern[] = new Pattern[andWord.split(" ").length];
            for (int i = 0; i < andWord.split(" ").length; i++) {
                pattern[i] = Pattern.compile(andWord.split(" ")[i]);
            }
            queryObject.append("t", new BasicDBObject().append("$in", pattern));
        }
        int skipCount = (p - 1) * ps;
        BasicDBObject sortDBObject = new BasicDBObject();
        sortDBObject.append(sb, so);
        System.out.println(queryObject);
        DBCursor dbCursor = collection.find(queryObject).sort(sortDBObject)
                .skip(skipCount).limit(ps);
        Map docInfo;
        while (dbCursor.hasNext()) {
            DBObject docObject = dbCursor.next();
            docInfo = new HashMap();
            docInfo.put("_id", (Long) docObject.get("_id"));
            docInfo.put("ch", (Integer) docObject.get("ch"));
            docInfo.put("title", (String) docObject.get("t"));
            docInfo.put("url", (String) docObject.get("u"));
            docInfo.put("pt",
                    DateUtil.getDateStrFromTime(docObject.get("pt") == null ? 0
                            : (Integer) docObject.get("pt")));
            docInfo.put("site_name", (String) docObject.get("sn"));
            docInfo.put("board_name", (String) docObject.get("bn"));
            result.add(docInfo);
        }

Mongo 编辑数组中的内容
删除tag数组中的一组
添加cleartag中一组数据

Map<String,String> cleartag=new HashMap<String, String>();
        cleartag.put("t",(String)condition.get("tag"));
        cleartag.put("keyword", filterWord);
        WriteResult writeResult = collection.update(new BasicDBObject().append("_id", new BasicDBObject().append("$in", filterResult)),
                new BasicDBObject()
        .append("$pull",new BasicDBObject().append("tag",new BasicDBObject().append("t",(String)condition.get("tag"))))
        .append("$push", new BasicDBObject().append("cleartag",cleartag)),false,true);

你可能感兴趣的:(工作之旅)