【发布时间】:2020-05-21 18:18:57
【问题描述】:
当我在 StackOverflow 上解决我的问题时,我注意到之前也有人问过同样的问题,但他们都没有得到很好的回应或实际答案。
Mongoose Find One on Nested Object
How can I find nested objects in a document?
回到我的问题:我想找到嵌套在架构中的对象。尝试 findMany 给出所有对象,而 findOne 只给出第一个对象,但我想要其 id 通过 req.body.checkbox 传递的特定对象。 我的 JS 代码就像..
app.post("/data", uploads, function (req, res) {
User.findById(req.user.id, function (err, foundUser) {
if (err) {
console.log(err);
} else {
if (foundUser) {
var checkedBox = req.body.checkbox;
console.log(checkedBox);
User.findMany({_id:foundUser._id},{comments:{$elemMatch:{_id:checkedBox}}} ,function(err,checkedobj){
if(err){
console.log(err);
}
else{
console.log(checkedobj.comments);
if (Array.isArray(checkedobj.comments)) {
res.render("checkout",{SIMG: checkedobj.comments});
} else {
res.render("checkout",{SIMG: [checkedobj.comments]});
}
}
})
}
}
});
});
这是我的架构,供参考
const commentSchema = new mongoose.Schema({
comment: String,
imagename: String,
permission:{type:Number,default:0},
});
const Comment = new mongoose.model("Comment", commentSchema);
const userSchema = new mongoose.Schema({
firstname: String,
lastname: String,
email: String,
password: String,
comments: [commentSchema],
permission:{type:Number,default:0},
});
userSchema.plugin(passportLocalMongoose);
const User = new mongoose.model("User", userSchema);
示例
{
"_id" : ObjectId("5ec3f54adfaa1560c0f97cbf"),
"firstname" : "q",
"lastname" : "q",
"username" : "q@q.com",
"salt" : "***",
"hash" : "***",
"__v" : NumberInt(2),
"comments" : [
{
"permission" : NumberInt(0),
"_id" : ObjectId("5ec511e54db483837885793f"),
"comment" : "hi",
"imagename" : "image-1589973477170.PNG"
}
],
"permission" : NumberInt(1)
}
当我选中 3 个复选框时,console.log(checkBox) 日志:
[
'5ec543d351e2db83481e878e',
'5ec589369d3e9b606446b776',
'5ec6463c4df40f79e8f1783b'
]
但是 console.log(checkedobj.cmets) 只给出一个对象。
[
{
permission: 0,
_id: 5ec543d351e2db83481e878e,
comment: 'q',
imagename: 'image-1589986259358.jpeg'
}
]
【问题讨论】:
-
您期望的结果形式是什么?
-
我希望传递我找到后得到的所有对象,并通过 SIMG 传递给 ejs。基本上checkedobj.cmets 应该记录有关所选对象的信息。
-
你能把console.log的输出(checkedBox);在你的问题?
-
再次编辑了问题。希望对您有所帮助。