Spring + MongoDB 利用MongoTemplate查询的问题
伊谢尔伦
伊谢尔伦 2017-04-24 09:13:09
[MongoDB讨论组]

db中有符合该条件的数据。

Query query=new Query(
    Criteria.where("AAA").is(XXobj.getAAA()).
    orOperator(Criteria.where("BBB").is(XXobj.getBBB()))
    );

find()方法:

List<XXObject> result = mongoTemplate.find(query, XXObject.class);
if(result!=null && !result.isEmpty()){
    return result.get(0);
}

findOne()方法:

XXObject obj = mongoTemplate.findOne(query, XXObject.class);
if(obj!=null){
    return obj;
}

问:
为什么同样的条件,这两个查询的结果却不同?(我遇到的问题是findOne查询结果为空)。

附:
官方文档对findOne和find的说明:
findOne Map the results of an ad-hoc query on the collection to a single instance of an object of the specified type.

find Map the results of an ad-hoc query on the collection to a List of the specified type.

看起来没什么区别啊。

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号