我想为下面的mongoDB查询生成java代码:
db.getCollection('dum_stats').find({ "engineNo": {$eq: 1} },{"sensorName": 1, "_id": false})
解决方案:
Bson query = Filters.eq("engineNo", Integer.parseInt(machineId));
FindIterable<Document> listOfSensorNames = dbCollection.find(query).projection(Projections.fields(Projections.include("sensorName"), Projections.exclude("_id")));
答案 0 :(得分:0)
此链接显示了java的所有示例: http://www.mkyong.com/mongodb/java-mongodb-hello-world-example/
答案 1 :(得分:0)
这真的取决于你用来连接mongo的lib!我们至少有这么多的图书馆!
1. Morphia. Type-Safe Wrapper with DAO/Datastore abstractions.
2. Spring MongoDB. Provides Spring users with a familiar data access features including rich POJO mapping.
3. Morphium. Feature-rich POJO Mapper including features like declarative caching, cluster awareness, validation, partial updates supports aggregation framework.
4. Mungbean (w/clojure support).
5. DataNucleus JPA/JDO. JPA/JDO wrapper
6. lib-mongomapper. JavaBean Mapper (No annotations).
7. MongoJack. Uses jackson (annotations) to map to/from POJOs and has a simple wrapper around DBCollection to simply this.
8. Kundera. JPA compliant ORM. Works with multiple datastores.
9. MongoFS. Enhanced file storage library with support for file compression, encryption, and Zip file expansion. Can be used on top of a GridFS-compatible bucket.
9. Jongo. Query in Java as in mongo shell (using strings), unmarshall results into Java objects (using Jackson)
10. MongoLink. Object Document Mapper (ODM.) Uses a plain java DSL for mapping declaration.
11. Hibernate OGM. Provides Java Persistence support for MongoDB.
12. Morphix. Lightweight, easy-to-use POJO mapper, with object caching and lifecycle methods.
<强>更新强> 您使用mongo java驱动程序的请求代码如下所示:
db.getCollection("dum_stats").find(
eq("engineNo", "1")
).projection(
fields(
include("sensorName"),
excludeId("_id")
)
);