spring 如何使用 Springs MongoTemplate 和 Criteria 类检索字段子集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10396561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:06:34  来源:igfitidea点击:

How do I retrieve a subset of fields using Springs MongoTemplate and Criteria class

springmongodbspring-mongo

提问by JARC

I want to be able to execute the following console command to return all rows with only a subset of fields populated but using Spring's MongoTemplateclass:

我希望能够执行以下控制台命令以返回仅填充了一部分字段但使用 SpringMongoTemplate类的所有行:

Console Command

控制台命令

db.person.find(null,{name:1})

db.person.find(null,{name:1})

MongoTemplate

蒙戈模板

mongoTemplate.find(new Query(...), Person.class)

mongoTemplate.find(new Query(...), Person.class)

Info on subset queries can be found here if you're interested http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields

如果您有兴趣,可以在此处找到有关子集查询的信息 http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields

Any ideas?

有任何想法吗?

Cheers

干杯

回答by JARC

Query q = new Query();
q.fields().include("name");
mongoTemplate.find(q, Person.class);

回答by dan andrei zaharia

mongoTemplate.getCollection(COLLECTION).find(null, new BasicDBObject(FIELD, "1"))