node.js - Mongoose find field in array not working -
i've application running in node.js , mongodb mongoose driver. i've created login function , debugging purpose, print out document after querying. here code:
mongodb:
{ "_id" : objectid("532cec102e42bcfced71fe03"), "admins" : [ { "username" : "matthew", "name" : "lol", "hidden" : { "password" : "f8d822f6fab9dfc3c7b3a5035cdc422aee3bb838", "access" : true } }, { "username" : "matthew1", "name" : "2000", "hidden" : { "password" : "40bd001563085fc35165329ea1ff5c5ecbdbbeef", "access" : false } } ], "location" : "sz", "hidden" : { "global_access" : true }, "token" : "f10e2821bbbea527ea02200352313bc059445190" }, { "_id" : objectid("53307831a6b6ecd28c5cacd7"), "admins" : [ { "username" : "matthew123123", "name" : "lol", "hidden" : { "password" : "f8d822f6fab9dfc3c7b3a5035cdc422aee3bb838", "access" : true } }, { "username" : "matthew1222222", "name" : "2000", "hidden" : { "password" : "40bd001563085fc35165329ea1ff5c5ecbdbbeef", "access" : false } } ], "location" : "sh", "hidden" : { "global_access" : true }, "token" : "f10e2821bbbea527ea02200352313bc159445190" }
branches.js
var mongoose = require('mongoose'); var db = mongoose.connection; var adminschema = new mongoose.schema({ username: string, name: string, hidden: { password: string, access: boolean } }); var branchschema = new mongoose.schema({ admins: [adminschema], location: string, hidden: { global_access: boolean }, token: string }); var branch = mongoose.model('branch', branchschema); var admin = mongoose.model('admin', adminschema); exports.login = function(req,res){ var user = new admin(req.body, null, true) user.hidden.password = sha1(user.hidden.password); console.log(user); mongoose.connect('mongodb://localhost:27017/branches'); db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function callback() { console.log('connected database'); branch.findone({ }, { "admins": { $elemmatch : { username: user.username, 'hidden.password': user.hidden.password, } } }) .exec( function (err,doc){ db.close(); res.send(doc); }); }); };
i made request sending in data:
{ username:'matthew', hidden.password:'200169+' }
and returned result:
{ "_id": "532cec102e42bcfced71fe03", "admins": [] }
however, expected see result this:
{ "admins": [ { "username":"matthew, "name":"lol" } ], "token":"f10e2821bbbea527ea02200352313bc059445190" }
i suspect query wrong, since if send in random combinations of username , password, still gives same result. please help, thank you!
there's problem findone()
statement in code. syntax of findone() is:
db.collection.findone(<criteria>, <projection>)
if pass empty json criteria, equivalent not specifying criteria , mongodb returns first document according natural order of documents on disk. since query contains empty json query criteria , provides projection criteria, mongodb returns first document on disk , applies projection criteria on it. returned document doesn't match projection, getting empty array.
you should try like:
... branch.findone({ "admins": { $elemmatch : { username: user.username, 'hidden.password': user.hidden.password, } } }, {"admins.$":1, token:1} ) ...
Comments
Post a Comment