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();

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐