簡體   English   中英

使用 NodeJS 從 MongoDB 中的集合中獲取數據

[英]get Data from a collection in MongoDB using NodeJS

我試圖通過過濾嵌套對象從 Mongo DB 獲取數據。

集合結構是:

    { 
   "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);
});

但我得到了空數組,如果有人能指出我正確的方向,我將不勝感激

MongoDB 在值之前比較類型 如果您的id來自req.params它可能作為字符串傳遞而repo.id似乎是一個數字。 嘗試將您的值轉換為數字:

const id = +req.params.repoId

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM