title: Mongoose Mongodb querying an array of objects
body: <p>I am struggling to get what I expect to see when querying an array using Mongoose. A user can have many rooms associated with his/her account. The room objects are stored in an array attached to the user within the collection. There is only one collection called users.</p>
  
  <p>Consider the following two Schemas for user and room:</p>
  
  <p>USER SCHEMA</p>
  
  <pre><code>var userSchema = mongoose.Schema({
  
  local            : {
      username     : String,
      email        : String,
      password     : String,
      rooms        : {type:Array, default: []}
  }  
  
  
  });
  </code></pre>
  
  <p>ROOM SCHEMA</p>
  
  <pre><code>var roomSchema = mongoose.Schema({
  
       name: String
  
  });
  </code></pre>
  
  <p>This is the query I have tried:</p>
  
  <pre><code>var User = require('../models/user');
  
  User.find({ 'rooms.name' : req.body.username  }, 
              { rooms: 
                  { $elemMatch : 
                     { 
                       name: req.body.username 
                     } 
                  } 
              }, function (err, user) {
  
          if (err){
              return done(err);
          }    
  
          console.log("user:::", user);
  
          if (user) {
              console.log("ROOM NAME FOUND");
              req.roomNameAlreadyInUse = true;
              next();
  
          } else {
              req.roomNameAlreadyInUse = false;
              console.log("ROOM NAME NOT FOUND");
              next();
  
          }
  
      });
  </code></pre>
  
  <p>The problem is this query seems to always return an empty array even if it should contiain something or if it should return nothing. So how would I search through all users room arrays to see if the room name already exists?</p>
  
  <p>I have also tried the following to queries but neither are successful:</p>
  
  <pre><code>User.find({local: {rooms: {$elemMatch: {name: req.body.username}}}}, function (err,     user) {
  
  }
  
  
  
  User.find({'local.rooms': {$elemMatch: {name: req.body.username}}}, function (err, user) {
  
  }
  </code></pre>
  
  <p>I should also point out that the database is populated as I can see the array (which has 1 element created on user account creation) in robomongo.</p>
  
  <p>Sample data from console output to show database is being populated (including user.rooms array):</p>
  
  <blockquote>
    <p>db.users.find()
    { "_id" : ObjectId("533c4db2b2c311a81be8a256"), "local" : { "password" : "$2a$08$0yQQJ2y0726kZtkWY5mAPOgZhacOmZn0Fd8DlausiuMB
    XE4ZblTXS", "username" : "paul", "email" : "test", "rooms" : [  {  "name" : "paul",  "<em>id" : ObjectId("533c4db2b2c311a81be8a2
    57") } ], "status" : "active" }, "</em>_v" : 0 }
    { "_id" : ObjectId("533c4ddab2c311a81be8a258"), "local" : { "password" : "$2a$08$dC3CbDTkG5ozECDTu/IicO3Az0WdkzlGh2xDcb8j1CF/
    FQhe5guZq", "username" : "john", "email" : "test2", "rooms" : [  {  "name" : "john",  "<em>id" : ObjectId("533c4ddab2c311a81be8a
    259") } ], "status" : "active" }, "</em>_v" : 0 }</p>
  </blockquote>