我正在努力获得使用Mongoose查询数组时期望看到的内容.用户可以拥有许多与他/她的帐户关联的房间.房间对象存储在集合中附加到用户的数组中.只有一个叫用户的集合.
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.
为用户和房间考虑以下两个模式:
Consider the following two Schemas for user and room:
用户模式
var userSchema = mongoose.Schema({ local : { username : String, email : String, password : String, rooms : {type:Array, default: []} } });房间图式
var roomSchema = mongoose.Schema({ name: String });这是我尝试过的查询:
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 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?
我也尝试过以下查询,但都没有成功:
I have also tried the following to queries but neither are 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) { }我还应该指出,因为我可以看到robomongo中的数组(在创建用户帐户时创建了1个元素),所以该数据库已被填充.
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.
正在填充来自控制台输出以显示数据库的示例数据(包括user.rooms数组):
Sample data from console output to show database is being populated (including user.rooms array):
db.users.find() {"_id":ObjectId("533c4db2b2c311a81be8a256"),本地":{密码":"$ 2a $ 08 $ 0yQQJ2y0726kZtkWY5mAPOgZhacOmZn0Fd8DlausiuMB XE4ZblTXS,"用户名:"保罗,"电子邮件:"测试,"房间:[{"名称:"保罗," id:ObjectId(" 533c4db2b2c311a81be8a2 57)}]," status:"活动}," _v:0} {"_id":ObjectId("533c4ddab2c311a81be8a258"),本地":{密码":"$ 2a $ 08 $ dC3CbDTkG5ozECDTu/IicO3Az0WdkzlGh2xDcb8j1CF/ FQhe5guZq",用户名":"john",电子邮件":"test2",房间":[{名称":"john"," id":ObjectId("533c4ddab2c311a81be8a 259)}],"状态:"活动}," _v:0}
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 }
推荐答案
我将其更改为使用findOne而不是find,并且现在可以使用.我不太确定为什么这会有所作为.这是我使用的findOne函数:
I changed it to use findOne instead of find and it works now. I am not too sure why this should make a difference. This is the findOne function I used:
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(); } });更多推荐
Mongoose Mongodb查询对象数组
发布评论