命令及java代码两种方式:mongo中查询某字段是否包含指定字符串

本文介绍了如何使用MongoDB的查询命令在`genres`字段中筛选包含特定字符串(如'Children'和'Comedy')的电影记录。通过shell命令和Java代码展示了如何进行匹配操作,帮助理解数据库查询的基本用法。
摘要由CSDN通过智能技术生成

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值