命令及java代码两种方式:mongo中查询某字段是否包含指定字符串
java mongo中查询某字段是否包含指定字符串
1 查询genres字段值(举例:"Action|Animation|Drama|Fantasy|Sci-Fi")中,包含Children的记录
shell命令,下面两种形式均可:
1)db.Movie.find({"genres":/Comedy/})
2)db.Movie.find({"genres":{$regex:"Children"}})
java代码:
MongoCursor<Document> cursor = collection.find(new BasicDBObject("genres", new BasicDBObject("$regex", "Children"))).iterator()
2查询genres字段值(举例:"Action|Animation|Drama|Fantasy|Comedy")中,包含Children并且包含Comedy的记录
shell命令,下面两种形式均可:
1)db.Movie.find({$and:[{"genres":/Children/},{"genres":/Comedy/}]})
2)db.Movie.find({$and:[{"genres":{$regex:"Children"}},{"genres":{$regex:"Comedy"}}]})
java代码:
BasicDBList condList = new BasicDBList();
condList.add(new BasicDBObject("genres", new BasicDBObject("$regex", "Children")));
condList.add(new BasicDBObject("genres", new BasicDBObject("$regex", "Comedy")));
MongoCursor<Document> cursor = collection.find(new BasicDBObject("$and", condList)).iterator();
更多推荐
所有评论(0)