0

I used $not command like this:

var nickname = 'jony';     
Chat.find({
  $or: [
    { 'nick': socket.nickname },
    { 'to': socket.nickname }
  ],
  dem: { $not: /"'+nickname+'"/ }
  },
  function (err, docs) {});

When I used like shown below it worked, but when I tried to put a variable like shown above it doesn't work.

dem : { $not: /jony/ }

How to put a variable in the $not operation above?

Andreas Hultgren
  • 14,763
  • 4
  • 44
  • 48
Manohar Gunturu
  • 125
  • 3
  • 16
  • 1
    possible duplicate of [MongoJS not returning data when searching with regular expressions](http://stackoverflow.com/questions/11597205/mongojs-not-returning-data-when-searching-with-regular-expressions) – JohnnyHK Feb 17 '14 at 17:37
  • 1
    possible duplicate of [How do you pass a variable to a Regular Expression JavaScript?](http://stackoverflow.com/questions/494035/how-do-you-pass-a-variable-to-a-regular-expression-javascript) – Jonathan Lonowski Feb 17 '14 at 17:39
  • 1
    JavaScript's regular expression literals, like `/jony/`, don't currently support concatenation or interpolation, as you might find in other languages. You'll have to construct a `RegExp` providing the pattern as a `String` argument -- `{ $not: new RegExp(nickname) }`. – Jonathan Lonowski Feb 17 '14 at 17:42

0 Answers0