Results 1 to 2 of 2

Thread: MongoDB regex query ?

  1. #1
    Join Date
    May 2006
    Location
    Germany
    Posts
    33

    Question MongoDB regex query ?

    Hi mongoDB users,

    from mongo shell I can execute the following query with no problem

    db.item.find({ "label" : { "$not" : /12/i }})

    how can I build the equivalent query using the mongo java driver oder spring-data mongo API ?

    if I do:
    DBObject tst = Criteria.where("label").not().regex("12").getCrite riaObject();
    I get:
    { "label" : { "$not" : { "$regex" : "12"}}}

    This query returns no result when executed from my java unit test.

    If I execute the following from the mongo shell

    db.item.find({ "label" : { "$not" : { "$regex" : "12"}}})

    I get the following error:

    error: {
    "$err" : "can't use $not with $regex, use BSON regex type instead",
    "code" : 13032

    Can anyone help me here ?

    Regards,
    Dirk

  2. #2
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    483

    Default

    I guess you can't simply negate the $regex operator. Have you thought about simply defining a negating regex in the first place?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •