mongoose - Mongooose query where ObjectId is null? -
how can search document doing .findone
objectid
field not set? cannot find if should searching on null
or undefined
or else.
in example below, i'm trying find document "email" value known userid has not yet been set:
var joinrequest = new mongoose.schema({ email: { type: string, unique: true, lowercase: true, trim: true }, code: { type: string, uppercase: true, trim: true, select: false }, lastsent: { type: date }, userid: { type: mongoose.schema.types.objectid, select: false } });
then again, can objectid
field ever null? should use string
here?
few things undefined
in context of mongodb
properties value undefined
not stored. following have no a
property
db.insert({a : undefined})
however arrays undefined
values converted null
db.insert({a : [undefined]}) //stores {a : [null]}
also undefined
has weird behaviors when used condition
db.users.find({a : undefined}) //finds db.users.findone({a : undefined}) //always returns first document (which problem you) db.users.update({a : undefined}, {a : true}) //only updates documents no property
so avoid use of undefined
, pretend doesn't exist. use null
instead since stored , does't dropped condition.
so example
db.users.insert({email : "email@domain.com", userid : null}); db.users.findone({email : "email@domain.com", userid : null});
if decide use undefined though this
db.users.insert({email : "email@domain.com"}); db.users.findone({email : "email@domain.com", userid : { exists : false }}); //works null
http://docs.mongodb.org/manual/reference/operator/query/exists/
Comments
Post a Comment