MongoDB regex search with wildcard

198 views
Skip to first unread message

janwen lou

unread,
Mar 3, 2015, 7:58:42 AM3/3/15
to mongod...@googlegroups.com
db.AgentCustomer.find({ agent_customer_name: { $regex: "^梁*",$options:"i"}} )




want to use above query to search field "agent_customer_name" start with '梁',but i got those results as follow,obviously some results are wrong,try another query:
db.AgentCustomer.find({ agent_customer_name: { $regex: "/^梁*/",$options:"i"}} )



got 0 document,what i missed in my query,thanks?



 
/* 0 */
{
    "_id" : ObjectId("54daf717582cb910e31d5043"),
    "_class" : "com.cyberdis.hands.model.mongo.MongoAgentCustomer",
    "agent_id" : "0588389BA8234BF1A18014754A249865",
    "agent_customer_id" : "E66A0D58034049299A3ED8CA1C381886",
    "agent_customer_name" : "梁山泊",
    "updateTime" : ISODate("2015-02-11T06:30:47.845Z")
}

/* 1 */
{
    "_id" : ObjectId("54daf813582cb910e31d5044"),
    "_class" : "com.cyberdis.hands.model.mongo.MongoAgentCustomer",
    "agent_id" : "0588389BA8234BF1A18014754A249865",
    "agent_customer_id" : "D2C3B160914A477B975F8156FBAE1F94",
    "agent_customer_name" : "梁山泊汴京分部",
    "updateTime" : ISODate("2015-02-11T06:34:59.865Z")
}

/* 2 */
{
    "_id" : ObjectId("54dc07a5d8cf519292544a37"),
    "_class" : "com.cyberdis.hands.model.mongo.MongoAgentCustomer",
    "agent_id" : "B53D06A1412D4F29A7F66641B6675D77",
    "agent_customer_id" : "9D86B3E347AE4D10AC7E0976437ABAED",
    "agent_customer_name" : "天上人间",
    "updateTime" : ISODate("2015-02-12T01:53:40.982Z")
}

/* 3 */
{
    "_id" : ObjectId("54f02fd7582cdf7074c6a501"),
    "_class" : "com.cyberdis.hands.model.mongo.MongoAgentCustomer",
    "agent_id" : "0588389BA8234BF1A18014754A249865",
    "agent_customer_id" : "3BEB9DE5F5AD4A818B34985E6CEB360F",
    "agent_customer_name" : "锦绣商场",
    "updateTime" : ISODate("2015-02-27T08:50:31.583Z")
}

/* 4 */
{
    "_id" : ObjectId("54f02ff7582cdf7074c6a504"),
    "_class" : "com.cyberdis.hands.model.mongo.MongoAgentCustomer",
    "agent_id" : "0588389BA8234BF1A18014754A249865",
    "agent_customer_id" : "51338966E0BB4DC2BDDAB8DEEFFC274F",
    "agent_customer_name" : "梁山泊洛阳分部",
    "updateTime" : ISODate("2015-02-27T08:51:02.987Z")
}

/* 5 */
{
    "_id" : ObjectId("54f3e088582c921115fbd242"),
    "_class" : "com.cyberdis.hands.model.mongo.MongoAgentCustomer",
    "agent_id" : "B53D06A1412D4F29A7F66641B6675D77",
    "agent_customer_id" : "2857246D54124FE5934B5168BE76FA38",
    "agent_customer_name" : "水上人间",
    "updateTime" : ISODate("2015-03-02T04:01:12.198Z")
}

/* 6 */
{
    "_id" : ObjectId("54f4014c582c921115fbd243"),
    "_class" : "com.cyberdis.hands.model.mongo.MongoAgentCustomer",
    "agent_id" : "B53D06A1412D4F29A7F66641B6675D77",
    "agent_customer_id" : "0980C5DF1F8442F69C9617DB39AD1F58",
    "agent_customer_name" : "冰上人间",
    "updateTime" : ISODate("2015-03-02T06:21:00.278Z")
}

/* 7 */
{
    "_id" : ObjectId("54f402be582c921115fbd244"),
    "_class" : "com.cyberdis.hands.model.mongo.MongoAgentCustomer",
    "agent_id" : "B53D06A1412D4F29A7F66641B6675D77",
    "agent_customer_id" : "E8393DB80125483CB360B55DE88B4D74",
    "agent_customer_name" : "火上人间",
    "updateTime" : ISODate("2015-03-02T06:27:10.226Z")
}

/* 8 */
{
    "_id" : ObjectId("54f4133c582c921115fbd245"),
    "_class" : "com.cyberdis.hands.model.mongo.MongoAgentCustomer",
    "agent_id" : "B53D06A1412D4F29A7F66641B6675D77",
    "agent_customer_id" : "3CEE0A6EE4AF4DB091959A7ED3E543B9",
    "agent_customer_name" : "木上人间",
    "updateTime" : ISODate("2015-03-02T07:37:32.348Z")
}

/* 9 */
{
    "_id" : ObjectId("54f41534582c921115fbd246"),
    "_class" : "com.cyberdis.hands.model.mongo.MongoAgentCustomer",
    "agent_id" : "B53D06A1412D4F29A7F66641B6675D77",
    "agent_customer_id" : "0DC3B6F368644B4C884F18ED5C8C277F",
    "agent_customer_name" : "海上人间",
    "updateTime" : ISODate("2015-03-02T07:45:56.892Z")
}

/* 10 */
{
    "_id" : ObjectId("54f41843582c921115fbd247"),
    "_class" : "com.cyberdis.hands.model.mongo.MongoAgentCustomer",
    "agent_id" : "B53D06A1412D4F29A7F66641B6675D77",
    "agent_customer_id" : "666CE150C5DC462794A5A1F0E6111D1A",
    "agent_customer_name" : "山上人间",
    "updateTime" : ISODate("2015-03-02T07:58:59.492Z")
}

/* 11 */
{
    "_id" : ObjectId("54f41a26582c921115fbd248"),
    "_class" : "com.cyberdis.hands.model.mongo.MongoAgentCustomer",
    "agent_id" : "B53D06A1412D4F29A7F66641B6675D77",
    "agent_customer_id" : "E8028C49B0384EBB8A61DA39327E6D7E",
    "agent_customer_name" : "一品鼎盛",
    "updateTime" : ISODate("2015-03-02T08:07:02.092Z")
}

/* 12 */
{
    "_id" : ObjectId("54f41a9b582c921115fbd249"),
    "_class" : "com.cyberdis.hands.model.mongo.MongoAgentCustomer",
    "agent_id" : "0588389BA8234BF1A18014754A249865",
    "agent_customer_id" : "08722DCD9FBF4829B1BF672F6C862A10",
    "agent_customer_name" : "上悦城",
    "updateTime" : ISODate("2015-03-02T08:08:59.825Z")
}

Will Berkeley

unread,
Mar 3, 2015, 12:07:39 PM3/3/15
to mongod...@googlegroups.com
The regex syntax is PCRE, so * means "match the preceding pattern 0 or more times". If you want to match documents with agent_customer_name values beginning with 梁, use

db.AgentCustomer.find({ "agent_customer_name" : /^梁/i })

-Will
Reply all
Reply to author
Forward
0 new messages