[英]How to fetch all the records using the like query in mongodb using java
I want to fetch all the records whose employeeId starts with 123.For this, I thought of using like query with regex. 我想获取employeeId以123开头的所有记录。为此,我想到使用与regex一样的查询。
Sample Records in MongoDB:
============================
{name:"XYZ",employeeID : 123456}
{name:"ABC",employeeID : 123789}
{name:"DEF",employeeID : 214356}
After query, it should retrieve only first two records. 查询后,它应该只检索前两个记录。
Can anyone please help me out regarding this issue. 任何人都可以帮我解决这个问题。
Try this 尝试这个
Write Query like this 像这样写查询
db.getCollection('employee').find({ $where: "/^123.*/.test(this.employeeID)" }) db.getCollection('employee')。find({$ where:“/^123.* /。test(this.employeeID)”})
Result : 结果:
/* 1 */
{
"_id" : ObjectId("57d15ab13f239d775c5cc667"),
"name" : "XYZ",
"employeeID" : 123456
}
/* 2 */
{
"_id" : ObjectId("57d15ab13f239d775c5cc668"),
"name" : "ABC",
"employeeID" : 123789
}
Try this: 尝试这个:
db.getCollection('test').find({ employeeID: { $regex: /^123.*$/ } }) db.getCollection('test')。find({employeeID:{$ regex:/^123.*$/}})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.