Re: [mongoose] Trouble Searching for Boolean field that is Null or False

1,693 views
Skip to first unread message

Aaron Heckmann

unread,
Jun 30, 2012, 2:08:59 PM6/30/12
to mongoo...@googlegroups.com
I'd like to see the error received from the .in([null,false]) query; that would be the right way to query bc it can utilize indexes VS

    model.where('field').ne(true)

Chaining equals() doesn't work that way. The value passed in the last call is the only value used. Yay for confusing chaining :)

On Jun 30, 2012, at 12:44 AM, Chris Newby <new...@gmail.com> wrote:

I have a Boolean field that defaults to Null and seems to work fine otherwise, but I'm having trouble querying for docs where this field is either null or false. I've tried a few things, in particular:
- someSchema.where('someBooleanField').in([null, false])
- someSchema.where('someBooleanField').equals(null).equals(false)

The first throws an invalid query error, the second only returns 'false' results (and I think I understand why). I did try using a custom function with $where but got various errors.

So what is the best way to search a boolean for both null and false (or true)? Thanks, Chris

--
http://mongoosejs.com
http://github.com/learnboost/mongoose
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en

Chris Newby

unread,
Jun 30, 2012, 3:40:15 PM6/30/12
to mongoo...@googlegroups.com
Hey Aaron, here's the error I get + stack trace:
------------------------------------------------------------------- 
Error: invalid query
    at Promise.error (/git/smiley/node/beta/node_modules/mongoose/lib/promise.js:80:38)
    at cb (/git/smiley/node/beta/node_modules/mongoose/lib/query.js:912:29)
    at /git/smiley/node/beta/node_modules/mongoose/lib/utils.js:408:16
    at /git/smiley/node/beta/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:124:30
    at /git/smiley/node/beta/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:166:32
    at /git/smiley/node/beta/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:441:39
    at Cursor.close (/git/smiley/node/beta/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:687:5)
    at [object Object].<anonymous> (/git/smiley/node/beta/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:441:21)
    at [object Object].g (events.js:156:14)
    at [object Object].emit (events.js:88:20)

Here's my schema:
------------------------------------------------------------------- 
userSchema = new Schema
shortId:
type: String
unique: true
required: true
name:
first:
type: String
required: true
last:
type: String
required: true
email:
type: String
unique: true
required: true
password:
salt:
type: String
required: true
hash:
type: String
required: true
stats:
lastLogin:
type: Date
loginCount:
type: Number
archive:
type: Boolean
default: null

Also fwiw, here's what my query object looks like before passing it to schema.find:
-------------------------------------------------------------------
{ options: { populate: {} },
  safe: undefined,
  _conditions: { archive: { '$in': [Object] } },
  model:
   { [Function: model]
     modelName: 'user',
     findByShortId: [Function],
     archiveByShortId: [Function],
     authenticate: [Function] },
  op: 'find',
  _currPath: 'archive' }

Let me know if there's anything else I should provide, I'd be glad to.

I hadn't tried .ne(true), and it does give the desired result, but you say it won't utilize indices. I'm totally new to mongoose and mongo, can you point me to where this is explained in the docs (if it is)?

Also, on chaining .equals() ... it was pretty clear what was happening by inspecting the query object, so no worries, not too confusing ... In the docs there's an example that chains together .lte and .gte, so I was sorta crossing my fingers anyway when I tried chaining .equals :)

Aaron Heckmann

unread,
Jul 2, 2012, 11:43:44 AM7/2/12
to mongoo...@googlegroups.com
Correction, mongo uses any index that it can, but often performance is lower with $ne/$nin.

As for querying a Boolean type with an $in condition, mongoose is broken. Please open a ticket.

Chris Newby

unread,
Jul 7, 2012, 6:15:31 PM7/7/12
to mongoo...@googlegroups.com
Awesome, thanks. Will do.
Reply all
Reply to author
Forward
0 new messages