Operate MongoDB in java

1: No entity, MongoDB is not used

            1: Query according to id

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

            2: Specify the returned data

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;
        }
    }

 

Guess you like

Origin blog.csdn.net/qq_20594019/article/details/111560404