Mongoose Mongodb requests an array of objects

I am trying to get what I expect to see when querying an array using Mongoose. A user can have many rooms associated with his / her account. Room objects are stored in an array attached to the user in the collection. There is only one collection called users.

Consider the following two schemes for the user and the room:

USER DIAGRAM

var userSchema = mongoose.Schema({

local            : {
    username     : String,
    email        : String,
    password     : String,
    rooms        : {type:Array, default: []}
}  


});

DIAGRAM OF NUMBERS

var roomSchema = mongoose.Schema({

     name: String

});

This is the query I tried:

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();

        }

    });

The problem is that this query always returns an empty array, even if it should rewrite something or if it should not return anything. So, how would I search all arrays of user rooms to find out if a room name exists?

I also tried the following queries, but none of them were successful:

User.find({local: {rooms: {$elemMatch: {name: req.body.username}}}}, function (err,     user) {

}



User.find({'local.rooms': {$elemMatch: {name: req.body.username}}}, function (err, user) {

}

, , ( 1 , ) robomongo.

( user.rooms):

db.users.find() { "_id": ObjectId ( "533c4db2b2c311a81be8a256" ), "local": { "password": "$ 2a $08 $0yQQJ2y0726kZtkWY5mAPOgZhacOmZn0Fd8DlausiuMB XE4ZblTXS", "username": "paul", "email": "test", "rooms": [{ "name": "paul", "id": ObjectId ( "533c4db2b2c311a81be8a2 57" )}], "status": "active" }, "_v": 0} { "_id": ObjectId ( "533c4ddab2c311a81be8a258" ), "local": { "password": "$ 2a $08 $dC3CbDTkG5ozECDTu/IicO3Az0WdkzlGh2xDcb8j1CF/ FQhe5guZq", "username": "john", "email": "test2", "rooms": [{ "name": "john", "id": ObjectId ( "533c4ddab2c311a81be8a 259" )}], "status": "active" }, "_v": 0}

+4
1

, findOne find, . , . findOne, :

User.findOne({'local.rooms': {$elemMatch: {name: req.body.username}}}, function (err, user) {

        if (err){
            return done(err);
        }    

        if (user) {
            console.log("ROOM NAME FOUND");
            req.roomNameAlreadyInUse = true;
            next();

        } else {
            req.roomNameAlreadyInUse = false;
            console.log("ROOM NAME NOT FOUND");
            next();

        }

    });
+10

Source: https://habr.com/ru/post/1534614/


All Articles