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();
本文介绍了如何使用MongoDB的查询命令在`genres`字段中筛选包含特定字符串(如'Children'和'Comedy')的电影记录。通过shell命令和Java代码展示了如何进行匹配操作,帮助理解数据库查询的基本用法。
1万+

被折叠的 条评论
为什么被折叠?



