Node.js mongodb中集合中的访问字段
我正在使用nodejs和mongodbNode.js mongodb中集合中的访问字段,node.js,mongodb,Node.js,Mongodb,我正在使用nodejs和mongodb "image" : "comments" : "date" : "name" : "description" : "author" : { "id" : "username" : }, "user" "__v" } 这些是我的博客收藏中的字段。 我想访问用户名 我试着像上面那样去做,但我得到了错误。 如何从作者内部访问用户名看起来您输入了
"image" :
"comments" :
"date" :
"name" :
"description" :
"author" : {
"id" :
"username" :
},
"user"
"__v"
}
这些是我的博客收藏中的字段。
我想访问用户名
我试着像上面那样去做,但我得到了错误。
如何从作者内部访问用户名看起来您输入了错误。您正在使用account,但在架构中它是author。试试这个:
Blog.find({author.username:req.params.username}
参考:
你的错误是什么?您是否尝试了以下引用语:Blog.find{'account.username':req.params.username}?对不起,我在问题中错误地编写了帐户。我已将其更改为author now。您的意思是它正在工作吗?虽然您输入了一个错误,但您不应该仅仅得到任何错误。find将返回[],如果它不起作用,您看到的是什么?@srinivasy it's giving error抱歉,我错误地编写了有问题的帐户。我现在已将其更改为author
Blog.find({author.username:req.params.username}
// Blog Schema
{
"description" : { type : string },
"author" : {
"id" : string
"username" : string,
"type": { type : string } // auther is an object with a type property
}
}
// Blog find query
const username = req.params.username // user name
Blog.find({"author.username" : username }, (err, data) => {
if(err) {
// error handler
}else{
console.log(data)
}
}