1

I am trying to get data from Mongo DB by filtering a nested object.

the collection structure is :

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

I am trying to get data by repo id. my code is :

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

but I am getting empty array, would be grateful is someone can point me to the right direction

2
  • What is the value of id variable ? Commented Dec 16, 2019 at 5:55
  • @mickl id is dynamic I get it from a parmeter const id = req.params.repoId; Commented Dec 16, 2019 at 5:57

1 Answer 1

1

MongoDB compares types before values. If your id comes from req.params it's probably passed as string while repo.id seems to be a number. Try to convert your value to number:

const id = +req.params.repoId
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.