Node.js 使用NodeJS从MongoDB中的集合获取数据
我试图通过过滤嵌套对象从Mongo DB获取数据 收集结构为:Node.js 使用NodeJS从MongoDB中的集合获取数据,node.js,mongodb,express,Node.js,Mongodb,Express,我试图通过过滤嵌套对象从Mongo DB获取数据 收集结构为: { "id":"8820624457", "type":"CreateEvent", "actor":{ "id":27394937, "login":"Johnson0608", "display_login":"Johnson0608", "gravatar_id":"", "url":"https://api.github.com/use
{
"id":"8820624457",
"type":"CreateEvent",
"actor":{
"id":27394937,
"login":"Johnson0608",
"display_login":"Johnson0608",
"gravatar_id":"",
"url":"https://api.github.com/users/Johnson0608",
"avatar_url":"https://avatars.githubusercontent.com/u/27394937?"
},
"repo":{
"id":163744671,
"name":"Johnson0608/test",
"url":"https://api.github.com/repos/Johnson0608/test"
},
"payload":{
"ref":"master",
"ref_type":"branch",
"master_branch":"master",
"description":null,
"pusher_type":"user"
},
"public":true,
"created_at":"2019-01-01T15:00:00Z"
}
我正在尝试通过repo id获取数据。
我的代码是:
collection.find({'repo.id':id}).toArray(function(err, docs) {
console.log(id);
assert.equal(err, null);
console.log("Found the following records");
console.log(docs);
res.status(200).json({docs});
callback(docs);
});
但我得到的是空数组,如果有人能给我指出正确的方向,我将不胜感激。如果您的
id
来自req.params
,它可能作为字符串传递,而repo.id
似乎是一个数字。尝试将值转换为数字:
const id = +req.params.repoId
id
变量的值是多少?@mickl id是动态的,我从参数const id=req.params.repoId获得它;