Skip to content

Unexpeted results when using the "$contains" operator #205

Closed

Description

@pignion

Unless I'm misunderstanding the usage, the '$contains' operator does not seem to produce expected results.

In this example I add items all that have their "test" property set to various types, including some with a string that I want to search for:

var db = new loki('testdb');
db.addCollection('testcollection')
  .insert([
    {test:'zzz' },
    {test: ['zzz'] },
    {test: {aaa:'zzz'}},
    {test: null },
    {test: [null] },
    {test: false},
    {test: true},
    {test: 0 },
    {test: 123},
    {test: {}},
    {test: []}
  ]);

var results = db.getCollection('testcollection')
    .find({'test': {'$contains': 'zzz'}});

console.log(results);

After searching for items where the "test" property contains "zzz" I get the following result set:

[ 
    {"test":123, "meta":{"revision":0, "created":1438839466380, "version":0}, "$loki":9},
    {"test":0, "meta":{"revision":0, "created":1438839466380, "version":0}, "$loki":8},
    {"test":true, "meta":{"revision":0, "created":1438839466380, "version":0}, "$loki":7},
    {"test":false, "meta":{"revision":0, "created":1438839466380, "version":0}, "$loki":6},
    {"test":null, "meta":{"revision":0, "created":1438839466380, "version":0}, "$loki":4},
    {"test":["zzz"], "meta":{"revision":0, "created":1438839466380, "version":0}, "$loki":2},
    {"test":"zzz", "meta":{"revision":0, "created":1438839466380, "version":0}, "$loki":1}
]

That last two results seem reasonable since the value of foo "contains" the search string, but the rest don't make much sense to me.

Activity

  1. changed the title [-]Unexpeted results when using the "$contains" operator behaviour[/-] [+]Unexpeted results when using the "$contains" operator[/+] on Aug 6, 2015
  2. added this to the 1.4.0 Brach milestone on Aug 6, 2015
  3. pignion commented on Aug 6, 2015

    @pignion
    Author

    My hope in using $contains in the first place was that it would allow me to search for values that were at times arrays containing the search term, so hopefully the fix preserves this functionality. In other words, the example would return the following:

    [ 
        {"test":["zzz"], "meta":{"revision":0, "created":1438839466380, "version":0}, "$loki":2},
        {"test":"zzz", "meta":{"revision":0, "created":1438839466380, "version":0}, "$loki":1}
    ]
    
  4. obeliskos commented on Aug 6, 2015

    @obeliskos
    Collaborator

    I believe @KimSchneider added the implementation for those ops, so ideally he might fix this but i'm not sure if he is monitoring this. If not, I will take a look at it over the weekend to see if I can better understand what he was trying to do. Your expectation of behavior sounds reasonable.

  5. KimSchneider commented on Aug 7, 2015

    @KimSchneider
    Contributor

    I didn't change the default behaviour but that was a bug, you are right :)
    #206

  6. obeliskos commented on Aug 7, 2015

    @obeliskos
    Collaborator

    Thanks! Looks great and I believe it performs exactly as mentioned.

  7. techfort commented on Aug 7, 2015

    @techfort
    Owner

    @pignion i'll wait for you to confirm that $contains now works as expected.

  8. pignion commented on Aug 8, 2015

    @pignion
    Author

    👍
    Thanks @techfort and @KimSchneider, it works as exactly expected now.

  9. techfort commented on Aug 8, 2015

    @techfort
    Owner

    cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions