从fs.files投射一列

时间:2015-03-24 19:51:34

标签: mongodb spring-data-mongodb

我想执行此查询:

db.fs.files.find({}, {filename: 1, _id: 0 })

使用spring-data-mongodb(只获取所有文件名)。

有没有人知道如何解决这个问题?

提前致谢。

1 个答案:

答案 0 :(得分:2)

选项1

DBCollection fsCollection = template.getCollection("fs.files");
fsCollection.setObjectClass(null);
DBCursor cursor = fsCollection.find(new BasicDBObject(), new BasicDBObject("filename", 1).append("_id", 0));

while (cursor.hasNext()){
     final DBObject object = cursor.next();
     String filename = object.get("filename")

}

选项2

@Autowired
MongoTemplate template;

@Document(collection="fs.files")
public class GridFsFile{

    @Id
    private ObjectId id;
    public ObjectId getId() { return id; }

    private String filename;
    public String getFilename() { return filename; }
}

Query query = new Query();
query.fields().include("filename").exclude("_id");
template.find(query, GridFsFile.class)