我正在努力获得使用Mongoose查询数组时我期望看到的内容。用户可以拥有与他/她的帐户相关联的许多房间。房间对象存储在集合中附加到用户的阵列中。只有一个名为users的集合。
考虑用户和房间的以下两种模式:
USER SCHEMA
var userSchema = mongoose.Schema({
local : {
username : String,
email : String,
password : String,
rooms : {type:Array, default: []}
}
});
ROOM SCHEMA
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();
}
});
问题是这个查询似乎总是返回一个空数组,即使它应该包含某些东西,或者它应该什么都不返回。那么我如何搜索所有用户房间阵列以查看房间名称是否已经存在?
我还尝试了以下查询,但都没有成功:
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个元素)。
正在填充从控制台输出到show database的示例数据(包括user.rooms数组):
db.users.find() {" _id" :ObjectId(" 533c4db2b2c311a81be8a256")," local" :{"密码" :" $ 2a $ 08 $ 0yQQJ2y0726kZtkWY5mAPOgZhacOmZn0Fd8DlausiuMB XE4ZblTXS","用户名" :"保罗","电子邮件" :" test"," rooms" :[{" name" :" paul"," id" :ObjectId(" 533c4db2b2c311a81be8a2 57")}],"状态" :"活跃" }," _v" :0} {" _id" :ObjectId(" 533c4ddab2c311a81be8a258")," local" :{"密码" :" $ 2a $ 08 $ dC3CbDTkG5ozECDTu / IicO3Az0WdkzlGh2xDcb8j1CF / FQhe5guZq","用户名" :" john"," email" :" test2"," rooms" :[{" name" :" john"," id" :ObjectId(" 533c4ddab2c311a81be8a 259")}],"状态" :"活跃" }," _v" :0}
答案 0 :(得分:21)
我将其更改为使用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();
}
});
答案 1 :(得分:2)
findOne
返回单个文档,其中find
返回光标。一旦找到find的光标,您就结束了,没有其他文档了。
User.findOne({'local.rooms': {$elemMatch: {name: req.body.username}}}, (err,UserInfo) => {
if (err){
return res.status(400).json({Error:"Something went wrong please try again"})
}
if (UserInfo) {
console.log("ROOM NAME FOUND");
req.roomNameAlreadyInUse = true;
res.json({msg:"Room name found.",userData:UserInfo})
} else {
req.roomNameAlreadyInUse = false;
console.log("ROOM NAME NOT FOUND");
res.json({msg:"Room name not found."})
}
});