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