java中操作MongoDB

1:没有实体,没有使用MongoDB

            1:根据id查询

      BasicDBObject queryObject = new BasicDBObject("_id",new ObjectId(""));
        collection.find(queryObject);

            2:指定返回的数据

public static Object findById(MongoCollection collection, String id) {

        BasicDBObject queryObject = new BasicDBObject("_id",new ObjectId(""));
        collection.find(queryObject);


        //指定要查询的字段
        BasicDBObject field = new BasicDBObject();
//        field.put("intro", 1);

        FindIterable findIterable = collection.find(queryObject).projection(field);

        MongoCursor cursor = findIterable.iterator();
        if (cursor.hasNext()){
            return cursor.next();
        }else {
            return null;
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_20594019/article/details/111560404
今日推荐