单条件查询
使用BasicDBObject配置查询条件
List<AbstractMongoEntity> list = Lists.newArrayList();
BasicDBObject cond1 = new BasicDBObject();
cond1.append("_id", new ObjectId("5de39f20684014f1d8b8fa37"));
FindIterable<Document> findIterable =
mongoTemplate.getCollection("crawler_cjwt").find(cond1);
MongoCursor<Document> cursor = findIterable.iterator();
Document document = null;
CjwtMongoEntity question = null;
while (cursor.hasNext()) {
document = cursor.next();
question = mongoConverter.read(CjwtMongoEntity.class, document);
list.add(question);
}
System.out.println(question);
cursor.close();
返回的是一个指针,所以我们需要通过该指针遍历结果,并装进list中返回使用。
对应的mongo脚本:
db.crawler_cjwt.find({'_id':new ObjectId("5de39f20684014f1d8b8fa37")})
查询该集合所有结果
List<FgcxMongoEntity> list = Lists.newArrayList();
FindIterable<Document> findIterable = crawlMongoTemplate.getCollection("fgcx").find();
MongoCursor<Document> cursor = findIterable.iterator();
FgcxMongoEntity question = null;
while (cursor.hasNext()) {
question = mongoConverter.read(FgcxMongoEntity.class, cursor.next());
list.add(question);
}
cursor.close();
多条件查询
String cityClassifyId = "b2c7147e804b48f8af562fbe1f1c32f6";
IndustryRepoClassify cityClassify = industryRepoClassifyMapper.selectByPrimaryKey(cityClassifyId);
IndustryRepoClassify provinceClassify = industryRepoClassifyMapper.selectByPrimaryKey(cityClassify.getParentId());
IndustryRepoClassify bsznClassify = industryRepoClassifyMapper.selectByPrimaryKey(provinceClassify.getParentId());
MongoCollection<Document> collection = mongoTemplate.getCollection("bszn");
BasicDBObject filterCondition = new BasicDBObject();
filterCondition.append("province.value", provinceClassify.getClassifyName());
filterCondition.append("city.value", cityClassify.getClassifyName());
FindIterable<Document> findIterable = collection.find(filterCondition);
List<Document> res = (List<Document>) findIterable;
等价于Mongo脚本:
db.bszn.find({$and:[{'province.value': '某省份'}, {'city.value': '某城市'}]})
$in查询
List<String> idList = Arrays.asList("5a08fea2704f44dda6e3d0a25d89014a", "96e71f036fa74bceac41130902f8d2be");
BasicDBObject query = new BasicDBObject();
BasicDBList conditionList = new BasicDBList();
for (String id : idList) {
conditionList.add(id);
}
query.put("classify_id", new BasicDBObject("$in", conditionList));
FindIterable<Document> findIterable = mongoTemplate.getCollection("crawler_classify").find(query);
MongoCursor<Document> cursor = findIterable.iterator();
IndustryRepoClassify question = null;
List<IndustryRepoClassify> list = Lists.newArrayList();
while (cursor.hasNext()) {
question = mongoConverter.read(IndustryRepoClassify.class, cursor.next());
list.add(question);
}
cursor.close();
等价于Mongo脚本:
db.crawler_classify.find({'classify_id':
{$in:['5a08fea2704f44dda6e3d0a25d89014a', '96e71f036fa74bceac41130902f8d2be']}})
更新某个属性
Bson filter = new BasicDBObject().append("_id", new ObjectId("5def5b6d5f090d15618df344"));
BasicDBObject set = new BasicDBObject();
set.append("kobe.value","33,");
Bson update = new Document("$set",set);
crawlMongoTemplate.getCollection("test").updateOne(filter, update);
等价于Mongo脚本:
db.test.update({'_id': new ObjectId("5def5b6d5f090d15618df344")}, {$set:{'kobe.value':'33,'}},{})
用于接取Mongo数据的Java Bean定义注意事项
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import lombok.*;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.mapping.Field;
import java.util.List;
import java.util.Map;
@Data
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class CjwtMongoEntity {
private ObjectId id;
@Field
private Map url;
@Field
private Map source;
@Field
private Map time;
@Field("batch_number")
private Map batchNumber;
@Field
private Map province;
@Field("data_source")
private Map dataSource;
}
注意的点有两个,一是id直接定义为ObjectId类型就可以了,二是其他的属性需要与Mongo文档中对应,如果不是完全一致,则需要在@Field注解后面加上Mongo文档中的键名。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)